0 Replies Latest reply on Aug 17, 2010 10:20 PM by Sean Wu

    Tips for using DBUnit and TestNG in seam-gen generated project (Seam 2.2.0.GA)

    Sean Wu Newbie

      After struggling for a while, I finally got DBUnit and TestNG work with my seam-gen generated project. I use Seam 2.2.0.GA and there are some tips or workrounds, so I list them here for other guys who still suffer from it.
      First, you need to modify the puJndiName in components-test.properties:
      Change



      # These properties are used to replace Ant-style tokens in the component descriptor (components.xml) at runtime.
      jndiPattern=#{ejbName}/local
      debug=true
      seamBootstrapsPu=true
      seamEmfRef=#{entityManagerFactory}
      puJndiName=#{null}



      To


      # These properties are used to replace Ant-style tokens in the component descriptor (components.xml) at runtime.
      jndiPattern=#{ejbName}/local
      debug=true
      seamBootstrapsPu=true
      seamEmfRef=#{entityManagerFactory}
      puJndiName=java:/xxxEntityManagerFactory



      Where xxx is your project name. I think this is a seam-gen bug, it puts a null in the expression.
      Second, create a XxxTest.xml under test dir where Xxx is your entity, for example Person.



      <!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd" >
      <suite name="PersonTestSuite" verbose="2" parallel="false">
          <test name="PersonTest">
      
              <parameter name="datasourceJndiName" value="java:/DefaultDS"/>
              <parameter name="database" value="HSQL" />
      
              <classes>
                  <class name="com.mydomain.demoproject.test.PersonTest" />
              </classes>
      
          </test>
      </suite>


      Note, since the data source in the persistence-test.xml is the DefaultDS, we must use same value here.
      Third, create a flat xml data file for DBUnit, say, BaseData.xml under test dir:



      <?xml version="1.0" encoding="UTF-8"?>
      
      <dataset>
          <PERSON
              id="1"
              version="1"
              name="A" />
          <PERSON
              id="2"
              version="1"
              name="B" />
          <PERSON
              id="3"
              version="1"
              name="C" />
      
      </dataset>
      


      Fourth, create XXXTest.java, say PersonTest.java:



      public class PersonTest extends DBUnitSeamTest {
      
          @Override
          protected void prepareDBUnitOperations() {
              beforeTestOperations.add(new DataSetOperation("BaseData.xml"));
          }
      
          @Test
          public void testPersonList() throws Exception {
              new NonFacesRequest("/PersonList.xhtml") {
                 @Override
                 protected void renderResponse() throws Exception {
                     List persons = (List)getValue("#{personList.resultList}");
                     Iterator itr = persons.iterator();
                     while(itr.hasNext()) {
                         Person person = (Person)itr.next();
                         System.out.println(person.getId()+":"+person.getName());
                     }
                 }
              }.run();
          }
      
      }


      Note, you should care about the path of DataSetOperation. There is a bug in the seam-gen's build.xml, it will not copy the BaseData.xml to the test-build. We will fix it in the next step.
      Fifth, modify the buildtest target in the build.xml:
      Change



      <copy todir="${test.dir}" flatten="true">
                  <fileset dir="${src.test.dir}">
                      name="**/*Test.xml"/>
                  </fileset>
       </copy>


      To



      <copy todir="${test.dir}" flatten="true">
                  <fileset dir="${src.test.dir}">
                          <include name="**/*.xml"/>
                  </fileset>
       </copy>


      Note, this will copy any xml file to test-build, in this case, BaseData.xml
      Sixth, run the test, and you will get:



      1:A
      2:B
      3:C
      PASSED: testPersonList



      Now, you can list the person data in the DBUnit plat xml file using TestNG test. You can see BaseData.xml is copied to test-build dir with PersonTest.xml file.


      Conclusion:




      • Pay attention to components-test.properties, persistence-test.xml, there is some exception about jndi name related to this kind of configuration





      • Pay attention to build.xml, sometimes it will ignore your configuration file





      • Pay attention to resource path in your test code, otherwise DBUnit will throw exceptions like org.dbunit.dataset.DataSetException: java.net.MalformedURLException