Announcement Announcement Module
Collapse
No announcement yet.
Unit Tests for data access layer Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Unit Tests for data access layer

    I am about to embark on refactoring an existing J2EE application at work.
    The current application has a simple data access object that returns
    various objects queried from a DB2 database. There are unit tests
    associated with this code now but they query against a development
    instance of the database and assume that certain data already exists in
    the database. What approaches have others taken when writing unit tests
    for DAOs?

    -Jeff

  • #2
    I've found DBUnit to be helpful. It puts the DB in a known state before starting your tests. Worth checking out...

    Comment


    • #3
      You might be interested in the new org.springframework.test package, shipped in the mock jar with 1.1.1. This includes test superclasses that create and rollback a transaction. Very useful to get the DB in a known state, run tests, then verify what it's done using JDBC, without changing persistent state. I've found this hugely beneficial on a large client project.

      Comment


      • #4
        Originally posted by Rod Johnson
        You might be interested in the new org.springframework.test package, shipped in the mock jar with 1.1.1. This includes test superclasses that create and rollback a transaction. Very useful to get the DB in a known state, run tests, then verify what it's done using JDBC, without changing persistent state. I've found this hugely beneficial on a large client project.
        Rod! You are great! This is great thing. It *hugely* simplifies my tests. Thanks God I looked in this topic today . I just overlooked new classes. I thing that introduction of these superconvinient helper classes to spring should be noted in bold in change log. Some introduction in documentation would be great. I wonder if everyone interested in already noticed these helpers.

        Artur

        Comment


        • #5
          Is there any example on how to use the classes in org.springframework.test?

          Comment


          • #6
            You can also use the dbunit extension to JUnit (www.dbunit.org) to reset the database to a given state before each test case.
            It also provides methods to verify that the database data matches an expected set of values (additional assert methods).

            So far I've only used it in a small test application, where it worked perfectly.

            Comment


            • #7
              Originally posted by zliu
              Is there any example on how to use the classes in org.springframework.test?
              http://www.springframework.org/docs/...textTests.html

              It's simple just name Your dpendent objects in test case as they are named in context, add setters, extend AbstractTransactionalSpringContextTests, implement getConfigLocations() and that's all. It just works, all Your dependencies will be set and all Your methods will be executed within transaction and rolled back . This is just cool.

              Artur

              Comment


              • #8
                Originally posted by karaznie
                It's simple just name Your dpendent objects in test case as they are named in context, add setters, extend AbstractTransactionalSpringContextTests, implement getConfigLocations() and that's all.
                Artur
                This is very cool. The only minor annoyance is that I have to copy and paste large sections of my application context files to create the contexts for my test cases.

                My tests run within a webapp (JUnitEE). What I would like to do is create a webapp context for my test cases, and have my test case use the webapp context configuration, delegating up to the root context when a bean is undefined.

                Does anyone have pointers on how I could set this up?

                Comment


                • #9
                  Originally posted by cepage
                  Does anyone have pointers on how I could set this up?
                  Consider using spring's 1.1.1 import directive.

                  Artur

                  Comment


                  • #10
                    Originally posted by karaznie

                    Consider using spring's 1.1.1 import directive.

                    Artur
                    That worked perfectly! Thank you!

                    Comment


                    • #11
                      problem with TransactionProxyFactory?

                      I got AbstractTransactionalSpringContextTests worked in testing the DAO layer. But I had problem using it to test the Service layer. The difference is that the beans defined in service layer have a parent, which defines transaction manager and transaction properties.

                      <bean id="txProxyTemplate" lazy-init="true" class="org.springframework.transaction.interceptor .TransactionProxyFactoryBean">
                      <property name="transactionManager">
                      <ref bean="myTransactionManager"/>
                      </property>
                      <property name="transactionAttributes">
                      <props>
                      <prop key="*">PROPAGATION_REQUIRED,-Exception</prop>
                      </props>
                      </property>
                      </bean>

                      <bean id="myFooService" class="FooServiceImpl" parent="txProxyTemplate" />

                      The error log said
                      org.springframework.beans.factory.BeanCreationExce ption: Error creating bean with name 'myFooService' defined in class path resource [service-applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyExcep tion: Invalid property 'transactionManager' of bean class [FooServiceImpl]: Property 'transactionManager' is not writable

                      Thanks in advance.

                      Comment


                      • #12
                        I guess this is not related to this topic but, this is how I use the definitions: (You need to define an interface for the service)
                        Code:
                        	<bean id="txProxyTemplate"
                        		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
                        		abstract="true">
                        		<property name="transactionManager"><ref local="transactionManager"/></property> 
                        		<property name="transactionAttributes"> 
                        			<props> 
                        				<prop key="update_*">PROPAGATION_REQUIRED</prop> 
                        				<prop key="*">PROPAGATION_SUPPORTS,readOnly</prop> 
                        			</props> 
                        		</property>	
                        		<property name="preInterceptors">
                        			<list>
                        				<ref local="securityAdvisor"/>
                        			</list>
                        		</property>
                        	</bean>
                        
                            <bean id="myFooService" parent="txProxyTemplate" >
                        		<property name="proxyInterfaces"><value>FooService</value></property>
                        		<property name="target">
                        			<bean id="myFooServiceTarget" class="FooServiceImpl"> 
                        				<property name="xxxDAO"><ref local="xxxDAO"/></property> 
                        			</bean> 
                        		</property>
                            </bean>
                        HTH,
                        Turgay

                        Comment


                        • #13
                          Getting the error message when...

                          my test class extends from AbstractTransactionalSpringContextTests:

                          The error message is below:

                          Implicit super constructor AbstractTransactionalSpringContextTests() is undefined for default constructor.
                          Must define an explicit constructor.

                          Comment


                          • #14
                            Spring-Mock for SJDBC &amp; JdbcTemplate

                            Hi everyone. My question is along the same vein as the rest of this thread. I've been spending a lot of time using EasyMock for testing my DAO Impls and I LOVE the Spring-mock helper classes like AbstractJdbcTests, RowMapperTests, and JdbcTemplateTests. These are GREAT and everyone should know about them when doing unit testing.

                            My question is; is there any code being written to let people use the logic in these helper classes without copying and pasting the code all the time? I have spent a lot of time and energy copying and pasting pieces of the code from these classes into my unit tests and then adding my own necessary code to test our stuff. This is not the worst thing in the world but it is very tedious and also somewhat error prone. It would be fantastic if there were a wrapper around these classes to setup mock connections, datasources, resultsets, etc that I could import into my unit test class and just call methods like "addResultSetToTest" or "addExpectedRecordToResultSet" and other such things. I started doing something similar on my own but there are a ton of scenarios to test for and they all use the JdbcTemplate and other objects a little differently than the others.

                            So in short, is there such a wrapper out there now? If not, is anyone working on one. I'd be happy to help out and contribute if someone is working on such a thing. But if someone has done so already, I'd hate to waste time re-inventing the wheel.

                            THanks.

                            Comment


                            • #15
                              spring testframwork.

                              Originally posted by Rod Johnson View Post
                              You might be interested in the new org.springframework.test package, shipped in the mock jar with 1.1.1. This includes test superclasses that create and rollback a transaction. Very useful to get the DB in a known state, run tests, then verify what it's done using JDBC, without changing persistent state. I've found this hugely beneficial on a large client project.
                              Hi Rod Johnson,

                              I think springunit test framework is good but when it comes to create test cases for enterprise application it lags behind. I mean, how to use this framework if i want to test ejb's. For example we have an enterprise application running where the flow is some thing like this:

                              the entire application was developed using spring framework ofcourse. The controller invokes the delegator which invokes the session bean, which invokes the business object in turn invokes the DAO for database transaction. I would like to develop test case using springunit framework for this kind of scenario. Currently i tried using this framework by invoking the existing context file but all the beans declared are not being loaded and hence not able to run the test case successfully. Could u guide me in this regard as to how to effectively use the springunit test framework.

                              thanks in advance.

                              Comment

                              Working...
                              X