Jeté Sample: DAO

Home Overview
The method invocation sample is the dbTest1 test case defined in the system test configuration.  This sample file is located in sampleSystemTests/subtests/DBTest.xml.  


DBTest.xml Test Cases
The top level bean to be configured for the DBTest.xml test file is the test case bean.  This bean defines the appropriate actions taken for the test case, and the name of the particular test case (for junitreport functionality).  

This test case also highlights something that could be done with all test cases, which is setup and teardown actions.  These actions are exactly like the other actions, but are particularly useful when a test case defines other testcases to be run with standard JUnit setup and teardown behavior (e.g. MultiTestCase).

    <bean id="dbTest.1" class="org.jete.cases.ActionTestCase">
        <property name="name">
            <value>dbTest.good.load</value>
        </property>
        <property name="setupActions">
            <list>
                <ref bean="dbTest.setup.customer"/>
                <ref bean="dbTest.setup.user"/>
            </list>
        </property>
        <property name="testActions">
            <list>
                <ref bean="dbTest.find.user.action"/>
                <bean class="org.jete.model.MethodInvocationAction">
                    <property name="targetObject">
                        <ref bean="dbTest.customerManager"/>
                    </property>
                    <property name="targetMethod">
                        <value>findCustomer</value>
                    </property>
                    <property name="arguments">
                        <list>
                            <value>100</value>
                        </list>
                    </property>
                    <property name="validators">
                        <list>
                            <ref bean="dbTest.customer.validator"/>
                        </list>
                    </property>
                </bean>
            </list>
        </property>
    </bean>

Datastore and Company Specific Objects

For the actions to function properly the test defines a couple of company specific entities using standard Spring POJO configuration.  

The first entity is a standard dataSource as configured by Spring.  This particular database schema is included with the sample system test, and loaded by the system test build file.

The userManager entity is referenced by a MethodInvocationAction to retrieve a user from the datastore.

     <bean name="dataSource" class="com.mysql.jdbc.jdbc2.optional.MysqlDataSource">
        <property name="url">
            <value>jdbc:mysql://localhost/systemtestdb</value>
        </property>
        <property name="user">
            <value>root</value>
        </property>
        <property name="password">
            <value>admin</value>
        </property>
    </bean>

    <bean id="userManager" class="com.company.user.UserManager">
    </bean>

Actions

The test case references specific actions.  2 of the actions in this sample are DBSetupActions that load the specified data into the datastore.

The actions are as defined:
- dbTestAction1 loads customer information into the db using DBUnit
- dbTestAction2 loads user information into the db using DBUnit
- dbTestAction3 is a method invocation action that uses a user DAO (userManager) to retrieve the loaded user from the db.

The properties for 
DBSetupAction are:
- dataSource (configured above)
- dataSetWrapper (wrap common DBUnit DataSet functionality for configuration ease) or:
- dataSet (DBUnit datasets configured by the system tester)
- dbOperations (a predefined list of operations to perform on the db, more than one may be specified)

<!-- inline in dbTest.1--> 
              <bean class="org.jete.model.MethodInvocationAction">
                    <property name="targetObject">
                        <ref bean="dbTest.customerManager"/>
                    </property>
                    <property name="targetMethod">
                        <value>findCustomer</value>
                    </property>
                    <property name="arguments">
                        <list>
                            <value>100</value>
                        </list>
                    </property>
                    <property name="validators">
                        <list>
                            <ref bean="dbTest.customer.validator"/>
                        </list>
                    </property>
                </bean>

    <bean id="dbTest.find.user.action" class="org.jete.model.MethodInvocationAction">
        <property name="targetObject">
            <ref bean="dbTest.userManager"/>
        </property>
        <property name="targetMethod">
            <value>findUser</value>
        </property>
        <property name="arguments">
            <list>
                <value>100</value>
            </list>
        </property>
        <property name="validators">
            <list>
                <ref bean="dbTest.user.validator"/>
            </list>
        </property>
    </bean>

<!-- inline with dbTest.2-->
                <bean class="org.jete.db.DBSetupAction">
                    <property name="dataSource">
                        <ref bean="dataSource"/>
                    </property>
                    <property name="dataSetWrapper">
                        <bean class="org.jete.db.ReplacementDataSetWrapper">
                            <constructor-arg value="subtests/db/BadUser.xml"/>
                        </bean>
                    </property>
                    <property name="dbOperations">
                        <list>
                            <value>INSERT</value>
                        </list>
                    </property>
                </bean>

Validator
The validator is a 
MethodResultValidator that works with MethodResultActions (MethodInvocationAction implements this) by calling getResult() to retrieve the result object, and accessing the appropriate values using Spring's PropertyValue object.  

This validator examines the first and last name of the resulting User object.
    <bean id="dbTest.user.validator" class="org.jete.model.MethodResultValidator">
        <property name="propertyValues">
            <list>
                <bean class="org.springframework.beans.PropertyValue">
                    <constructor-arg><value>firstName</value></constructor-arg>
                    <constructor-arg><value>QA1</value></constructor-arg>
                </bean>
                <bean class="org.springframework.beans.PropertyValue">
                    <constructor-arg><value>lastName</value></constructor-arg>
                    <constructor-arg><value>Admin</value></constructor-arg>
                </bean>
            </list>
        </property>
    </bean>
hosted by SourceForge Logo