Today I struggled with getting my TestNG Integration Tests working in my Seam 2 project. It’s not a very smart idea to use the production/development database for database tests and here’s why:
1. It is damn slow
2. We want to separate „real“/fake data from unit test data.
3. Under normal circumstances we want a clean database or a database with predefined data (see )
4. We want the unit tests to run everywhere. It should not matter if we have a Windows machine, a Linux Buildserver or a MacOSX. It should just work. Oh and I forgot. We even want it to work without Internet Connection.
5. …
So I decided to use HSQLDB for my Unit tests.
The first barrier was getting TestNG working. The Seam approach is to use several small „*Test.xml“ files. Since I am a lazy programmer and I use IntelliJ Idea, I wanted a single „testng.xml“ file to be deployed instead of several „*Test.xml“ files. (I get smart testng.xml completion with IntelliJ, so I definitively want a single xml. So step one is to modify your build.xml
To do this: Just replace all occurrences of „*Test.xml“ with „testng.xml“.
Sweet, now we get our testng.xml file deployed with our other test files
Next Step is to replace the contents of persistence-test.xml with the following content:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | <?xml version="1.0" encoding="UTF-8"?> <!-- Persistence deployment descriptor for tests --> <persistence xmlns="http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0"> <persistence-unit name="testworld" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <non-jta-data-source>java:/DefaultDS</non-jta-data-source> <properties> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="false"/> <property name="hibernate.connection.driver_class" value="org.hsqldb.jdbcDriver"/> <property name="hibernate.connection.url" value="jdbc:hsqldb:."/> <property name="hibernate.connection.username" value="sa"/> <property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> </properties> </persistence-unit> </persistence> |
And that folks, is all the magic. Should be pretty straightforward. For those of you who are asking how to use the database in your tests, visit Vikas Blog, which was my main resource for this task. Please note the <non-jta-data-source>java:/DefaultDS</non-jta-data-source> line, because that small line was the missing piece for me.
Hey, how did you get this persistence-test.xml used in your project? Where did you place it?
Regards,
Marcio Lima
I used seam-gen to generate my project skeleton. When running the ant test target, the persistence-test.xml will be deployed as persistence.xml for the test. A good place to get started with seam-gen is here: http://docs.jboss.org/seam/latest/reference/en-US/html/gettingstarted.html#d0e2710
For existing projects, I would recommend to use seam-gen to scaffold a test project and copy the relevant parts from build.xml. The hard part would be to get embedded JBoss working with an existing project. Let me know if you need more information