I have worked on projects that uses Spring and unit testing with Spring was quite easy. Now I am working on Seam projects, so I wondered how can I do unit tests for my Repository classes with JPA.
I had some problems but here is a working environment in the end. ( it may have a few misunderstanding, I need to review it from the ref doc )
We have the regular src/main folder in our project.
under src/main, we have java and resources folder under as usual. I have jsut added the persistence.xml under resources/META-INF and it is as follows:
<?xml version="1.0" encoding="UTF-8"?> <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="testUnit" transaction-type="RESOURCE_LOCAL"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <class>my.package.class</class> ... <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQLDialect"/> <property name="hibernate.connection.url" value="jdbc:postgresql://localhost:5432/eniyiyemektest?charSet=utf-8"/> <property name="hibernate.connection.driver_class" value="org.postgresql.Driver"/> <property name="hibernate.connection.username" value="user"/> <property name="hibernate.connection.password" value="pass"/> <property name="hibernate.hbm2ddl.auto" value="create"/> <property name="hibernate.show.sql" value="true"/> </properties> </persistence-unit> </persistence>
and that is it. My components.xml is in under resources folder also. And it is as :
<?xml version="1.0" encoding="UTF-8"?> <components xmlns="http://jboss.com/products/seam/components" xmlns:core="http://jboss.com/products/seam/core" xmlns:persistence="http://jboss.com/products/seam/persistence" xmlns:transaction="http://jboss.com/products/seam/transaction" xmlns:security="http://jboss.com/products/seam/security" xmlns:web="http://jboss.com/products/seam/web" xmlns:mail="http://jboss.com/products/seam/mail" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation= "http://jboss.com/products/seam/core http://jboss.com/products/seam/core-2.1.xsd http://jboss.com/products/seam/persistence http://jboss.com/products/seam/persistence-2.1.xsd http://jboss.com/products/seam/transaction http://jboss.com/products/seam/transaction-2.1.xsd http://jboss.com/products/seam/security http://jboss.com/products/seam/security-2.1.xsd http://jboss.com/products/seam/web http://jboss.com/products/seam/web-2.1.xsd http://jboss.com/products/seam/mail http://jboss.com/products/seam/mail-2.1.xsd http://jboss.com/products/seam/components http://jboss.com/products/seam/components-2.1.xsd"> <core:manager conversation-timeout="120000" concurrent-request-timeout="500" conversation-id-parameter="cid"/> <!-- Persistence --> <transaction:entity-transaction entity-manager="#{entityManager}"/> <persistence:entity-manager-factory name="testUnit"/> <persistence:managed-persistence-context name="entityManager" auto-create="true" entity-manager-factory="#{testUnit}"/> </components>
so I am ready to write my tests. under src/test/java/my/package I add this following test code.
public class UserRepositoryTest extends SeamTest{ @Test public void testUserCreation() throws Exception{ new FacesRequest(){ @Override protected void invokeApplication() throws Exception { User user = new User(); user.setEmail( "test@mydomain.com" ); user.setPassword( "password" ); user.setFirstname( "firstname" ); user.setLastname( "lastname" ); Assert.assertNull( user.getId() ); UserRepository repo = (UserRepository) getInstance( "userRepository" ); Assert.assertNotNull( repo ); repo.create( user ); Assert.assertNotNull( user.getId() ); } }.run(); } }
and that is it. Small important checks are:
- Do not forget to extend from SeamTest class
- Use testNG not junit, I don’t why but junit didn’t work
- Use FacesRequest to run the tests, somwhow if you use ComponentTest class to run the tests ( which makes more sense also ) the transactions is not automatically done.