Integration test configuration error. Seam 2.2.0GA + Eclipse + TestNG + DBUnitTest
mmaia Mar 28, 2010 6:17 AMHi,
I'm new to Seam. Using Seam 2.2.0 GA with JBoss 5.1 and MySQL5. I have a straightforward implementation of login routine with Seam. I got my project generated by seam-gem and so far the login implementation is very simple with 2 entitys (User and Role) and an implemented AuthenticatorBean.authenticate method. Follows the code for authenticate method:
@SuppressWarnings("unchecked") public boolean authenticate() { log.info("authenticating {0}", credentials.getUsername()); List<User> resultado = entityManager .createQuery( "SELECT u FROM User u WHERE u.email=#{credentials.username} AND u.senha=#{credentials.password}") .getResultList(); if (resultado.size() == 0) { log.info("Autenticacao falhou"); return false; } else { user = (User) resultado.get(0); for (Role role : user.getRoles()) { identity.addRole(role.getNome()); } log.info("Usuario autenticado com sucesso"); return true; } }
Now I need to test this code. Because I'm using a query inside this method I believe(Any thoughts on this?) an integration test with DB simulated data is indicated. With this in mind I did a little studying and got a TestNGClass extending a DBUnitSeamTest:
public class LoginTest extends DBUnitSeamTest { @Override protected void prepareDBUnitOperations() { beforeTestOperations.add(new DataSetOperation("loginTest.xml")); } @Test public void integrationTestAuthenticate() throws Exception { new FacesRequest("#{identity.login}") { @Override protected void updateModelValues()throws Exception { setValue("#{credentials.username}", "maia.marcos@gmail.com"); setValue("#{credentials.password}", "12345678"); } @Override protected void invokeApplication() { assert getValue("#{credentials.username}").equals("maia.marcos@gmail.com"); assert invokeMethod("#{authenticator.authenticate()}").equals(true); } @Override protected void renderResponse() { User user = (User) getValue("#{user}"); assert user.getNome().equals("Admin"); } }.run(); } }
I have placed the loginTest.xml in the same package as LoginTest.class with DB data for this test, and it's contents follows:
<dataset> <USER id="1" email="maia.marcos@gmail.com" nome="Admin" senha="12345678"/> <USER id="2" email="anototudo@gmail.com" nome="Anototudo" senha="anototudo"/> <ROLE id="1" nome="ADMIN" descricao="desc testes role admin" /> <ROLE id="2" nome="DIETA_CALORIAS" descricao="desc testes role dieta_calorias" /> <USER_ROLE user_id="1" role_id="1"/> <USER_ROLE user_id="2" role_id="2"/> </dataset>
Also I have configured resources/META-INF/persistent-test.xml (I believe this is used but have no clue how so far), with DB data as follows:
<?xml version="1.0" encoding="UTF-8"?> <!-- Persistence deployment descriptor for dev profile --> <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="anototudo"> <provider>org.hibernate.ejb.HibernatePersistence</provider> <jta-data-source>java:/anototudoDatasource</jta-data-source> <properties> <property name="hibernate.dialect" value="org.hibernate.dialect.MySQLDialect"/> <property name="hibernate.hbm2ddl.auto" value="create-drop"/> <property name="hibernate.show_sql" value="true"/> <property name="hibernate.format_sql" value="true"/> <property name="hibernate.default_catalog" value="anototudo"/> <property name="hibernate.transaction.manager_lookup_class" value="org.hibernate.transaction.JBossTransactionManagerLookup"/> </properties> </persistence-unit>
I have also included all required libraries in TestNG run configuration for DB test and also because I'm using java 6 the VM argument is set: -Dsun.lang.ClassLoader.allowArraySyntax(equals)true
Finally I create a TestNG xml called IntegrationTests.xml configuration file and pointed to it in the run configuration under TestNG eclipse plugin: