How to configure a project with JUnit to be able to construct correctly inject the EntityManager?

1

I have a legacy JSF / Demoiselle 2.4 application that did not have the good practice of building unit tests. In order to satisfy new requirements, I have implementations to make and I intend to build them using TDD.

However, when writing my first test case I had problems with running a Hibernate query because EntityManager is not injected into object DAO (which is a derivative of JPACrud ).

At the base of the exception stack I have:

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

The persistence.xml file itself for JUnit is configured the same way as the application (where everything works). I think I'm missing something, but at the moment I can not identify.

I'm grateful for any help!

Update

After some studies, I realized that I could configure persistence.xml to no longer use a DataSource configured in JBoss and that the correct thing is to configure the connection parameters directly. After doing this and updating my pom.xml to include driver JDBC support for JUnit , the exception changed to:

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

So, I think the situation has evolved, but I still have to solve this case!

Does anyone have any tips?

Update 2

As requested, follow both the test class and pom.xml .

Test class

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>

Update 3

Follows as requested.

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>
    
asked by anonymous 04.05.2015 / 14:21

1 answer

1

Verify that persistence.xml for testing is at: / src / test / resources / META-INF with local connection settings and entity class declaration. Ex:

<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="bookmark-ds" transaction-type="RESOURCE_LOCAL">
    <class>org.demoiselle.bookmark.domain.Bookmark</class>
    <properties>
        <property name="javax.persistence.jdbc.driver" value="org.hsqldb.jdbcDriver" />
        <property name="javax.persistence.jdbc.user" value="sa" />
        <property name="javax.persistence.jdbc.password" value="" />
        <property name="javax.persistence.jdbc.url" value="jdbc:hsqldb:mem:." />
        <property name="eclipselink.logging.level" value="FINE" />
        <property name="eclipselink.ddl-generation" value="create-tables" />
        <property name="eclipselink.ddl-generation.output-mode" value="database" />
    </properties>
  </persistence-unit>
</persistence>

It will not be the same as in / src / main / resources / META-INF

    
04.05.2015 / 19:59