Tenho uma aplicação legada JSF/Demoiselle 2.4 que não apresentava a boa prática de construir testes unitários. De forma a satisfazer novos requisitos, tenho implementações a fazer e pretendo construí-las fazendo uso de TDD.
No entanto, ao escrever meu primeiro test case tive problemas com a execução de uma consulta Hibernate por que o EntityManager
não é injetado no objeto DAO
(que é um derivado de JPACrud
).
Na base da exception stack tenho:
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getNameParser(Unknown Source)
at org.hibernate.service.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:86)
... 104 more
O arquivo persistence.xml
próprio para o JUnit está configurado da mesma forma que o da aplicação (onde tudo funciona). Acredito que esteja me faltando algum detalhe, mas no momento não sou capaz de identificar.
Fico agradecido com qualquer auxílio!
Atualização
Após alguns estudos, percebi que poderia configurar a persistence.xml
para não mais fazer uso de um DataSource configurado no JBoss e que o correto é configurar os parâmetros de conexão diretamente. Após fazer isso e atualizar o meu pom.xml
para incluir o suporte ao driver JDBC
para o JUnit
, a exceção mudou para:
Caused by: javax.naming.NoInitialContextException: Need to specify class name in environment or system property, or as an applet parameter, or in an application resource file: java.naming.factory.initial
at javax.naming.spi.NamingManager.getInitialContext(Unknown Source)
at javax.naming.InitialContext.getDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getURLOrDefaultInitCtx(Unknown Source)
at javax.naming.InitialContext.getNameParser(Unknown Source)
at org.hibernate.service.jndi.internal.JndiServiceImpl.parseName(JndiServiceImpl.java:86)
... 79 more
Assim, acho que a situação evoluiu, mas ainda preciso solucionar este caso!
Alguém tem alguma dica?
Atualização 2
Como solicitado, seguem tanto a classe de testes quanto o pom.xml
.
Classe de testes
package br.ufpr.frequencia.scheduler;
import javax.inject.Inject;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import br.gov.frameworkdemoiselle.junit.DemoiselleRunner;
@RunWith(DemoiselleRunner.class)
public class SchedulerTests {
@Inject
FrequenciaScheduler scheduler;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
@Test
public void shouldAtualizarCarreiraEscalaComPredispostos() {
// Arranje
// Act
scheduler.atualizarCarreiraEscalasDePredispostos();
// Assert
}
}
POM.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>br.ufpr</groupId>
<artifactId>frequencia</artifactId>
<version>1.0.0-Dev</version>
<packaging>war</packaging>
<name></name>
<description></description>
<url></url>
<parent>
<groupId>br.gov.frameworkdemoiselle</groupId>
<artifactId>demoiselle-jsf-parent</artifactId>
<version>2.4.1</version>
</parent>
<dependencies>
<dependency>
<groupId>br.ufpr</groupId>
<artifactId>core</artifactId>
<version>1.0.8</version>
</dependency>
<!-- SECURITY - CAS -->
<dependency>
<groupId>br.ufpr</groupId>
<artifactId>Security_SCA_CAS_EJB2</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>org.jdom</groupId>
<artifactId>jdom2</artifactId>
<version>2.0.5</version>
</dependency>
<dependency>
<groupId>br.gov.frameworkdemoiselle</groupId>
<artifactId>demoiselle-jpa</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>br.gov.frameworkdemoiselle</groupId>
<artifactId>demoiselle-jta</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.primefaces</groupId>
<artifactId>primefaces</artifactId>
<version>5.0</version><!--$NO-MVN-MAN-VER$ -->
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.primefaces.extensions</groupId>
<artifactId>primefaces-extensions</artifactId>
<version>3.0.0</version>
</dependency>
<dependency>
<groupId>org.primefaces.themes</groupId>
<artifactId>cupertino</artifactId>
<version>1.0.1</version>
</dependency>
<dependency>
<groupId>br.gov.frameworkdemoiselle.component</groupId>
<artifactId>demoiselle-junit</artifactId>
<version>2.3.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-envers</artifactId>
<version>4.1.7.Final</version>
<exclusions>
<exclusion>
<groupId>antlr</groupId>
<artifactId>antlr</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.antlr</groupId>
<artifactId>antlr</artifactId>
<version>3.5.2</version>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.2.2</version>
</dependency>
<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.7.0</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.3.2</version>
</dependency>
<dependency>
<groupId>br.ufpr</groupId>
<artifactId>sigepeservice-client</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>br.ufpr</groupId>
<artifactId>sieservice-client</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>br.ufpr</groupId>
<artifactId>sigeuservice-client</artifactId>
<version>1.0.0</version>
</dependency>
<dependency>
<groupId>org.jboss.as</groupId>
<artifactId>jboss-as-ejb-client-bom</artifactId>
<version>7.1.3.Final</version>
<scope>provided</scope>
<type>pom</type>
</dependency>
<dependency>
<groupId>br.ufpr.doodle</groupId>
<artifactId>doodle-consumertool</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>com.oracle</groupId>
<artifactId>ojdbc14</artifactId>
<version>10.2.0.5.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<repositories>
<!-- Repositorio interno DSI UFPR -->
<repository>
<id>nexus</id>
<name>Nexus</name>
<url>http://homologa3.cce.ufpr.br:8080/nexus/content/groups/public</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
<releases>
<enabled>true</enabled>
</releases>
</repository>
</repositories>
</project>
Atualização 3
Segue conforme solicitado.
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.0"
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_2_0.xsd">
<persistence-unit name="xxx-ds" transaction-type="JTA">
<!-- Várias classes -->
<class>br.xxx.yyy.domain.zzz</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="oracle.jdbc.OracleDriver" />
<property name="javax.persistence.jdbc.user" value="XXXXXXX" />
<property name="javax.persistence.jdbc.password" value="*******" />
<property name="javax.persistence.jdbc.url" value="jdbc:oracle:thin:@server.service.br:1521:kk" />
<property name="hibernate.show_sql" value="true" />
<property name="hibernate.format_sql" value="true" />
<property name="hibernate.transaction.jta.platform"
value="org.hibernate.service.jta.platform.internal.JBossAppServerJtaPlatform" />
<property name="hibernate.dialect" value="org.hibernate.dialect.Oracle10gDialect" />
<!-- Envers properties -->
<property name="org.hibernate.envers.audit_table_prefix" value=""/>
<property name="org.hibernate.envers.audit_table_suffix" value="_AUD"/>
<property name="org.hibernate.envers.revision_field_name" value="versao"/>
<property name="org.hibernate.envers.revision_type_field_name" value="operacao"/>
<property name="org.hibernate.envers.store_data_at_delete" value="true"/>
</properties>
</persistence-unit>
</persistence>
persistence.xml