How to configure a project with Junit to be able to build correctly inject Entitymanager?

Asked

Viewed 669 times

1

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

However, when writing my first test case I had problems with running a Hibernate query because the EntityManager is not injected into the 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 archive persistence.xml proper to Junit is configured in the same way as the application (where everything works). I believe I’m missing some detail, but at the moment I’m not able to identify.

I’m grateful for any help!

Updating

After a few studies, I realized I could set up the persistence.xml to no longer make use of a Datasource configured in Jboss and that the correct is to configure the connection parameters directly. After doing this and updating my pom.xml to include driver support JDBC to the 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 need to solve this case!

Does anyone have any tips?

Update 2

As requested, they follow both the test class and the 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

Follow 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>
  • You can post the test class?

  • also pom.xml and test persistence.xml.

  • @Saito: details added

  • @Saito: added the code of persistence.xml

1 answer

1


Check that persistence.xml for testing is in: /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>

Won’t be the same as in /src/main/Resources/META-INF

  • Thanks for the information, but as I put in an update of the question, I had already realized this detail and made the change, which in fact changed the error situation, changing from one to another. However, there is still an error!

  • Hello, you missed persistence.xml. And since you’re using JTA in the project, make sure that Demoiselle.properties from tests /src/test/Resources is configured to use JPA. Upgrade Démoiselle-junit to 2.3.1 as well.

  • In your persistence.xml you are setting it to JTA (transaction-type="JTA") and the test with Junit will not work. You should use RESOURCE_LOCAL. Another recommendation you would make is not to use the connection to the oracle database. If we were to stick to the concept of unit testing, we should not even use a bench, but only "mocks". Testing with bench use would feature integrated testing. To facilitate the construction, we chose to use Hsqldb which is a very simple bank in the tests.

  • Thus, tests that are not affected by the characteristics of the bank can be created. But it doesn’t stop you from testing with Oracle, but you’ll have to set up without JTA.

  • Another justification for using hsqldb is that if there was a continuous integration environment (common in our internal environments), it would be even more recommended not to use an external connection, because if the server is out of the air, the tests would fail. In "embedded" way as in the use of Hsqldb there would be no such risk.

  • It seems that the last recommendation regarding the type of transaction (RESOURCE_LOCAL) was what was missing for everything to be happy. After this step failed to also remove the import exclusion from ANTLR along with Hinernate in POM.XML. Now the tests already run. I’d like to thank you for your willingness to help!

  • You’re welcome Alex, we’re here to help.

  • After a long time I resumed the question. Everything is working in my tests except data recording situations. What happens is that no transaction is being created and committed. As a result, certain test situations you consider are talking about in the tests. Any idea what’s missing?

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.