Unable to embed EntityManager in JPA integration testing with Arquillian and WildFly

I am trying to do integration testing with the following stack:

App server: Embedded WildFly
CDI container: Weld
Database: In-memory H2
ORM: Hibernate/JPA
Platform: Java 8
OS: Mac OS X 10.10

I installed basic integration testing with Arquillian (as done here ), and I can embed dependencies, but injection EntityManagerproves to be a problem. When dereferencing a field, the object manager always appears NullPointerException.

I have seen many articles (including this and this ), but I still cannot get this seemingly simple thing to work.

Please see below my pom.xml

  <dependencies>
    <dependency>
      <groupId>org.jboss.spec</groupId>
      <artifactId>jboss-javaee-7.0</artifactId>
      <version>1.0.0.Final</version>
      <type>pom</type>
      <scope>provided</scope>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.8.1</version>
      <scope>test</scope>
    </dependency>

    <!-- JUnit Container Implementation for the Arquillian Project -->
    <dependency>
      <groupId>org.jboss.arquillian.junit</groupId>
      <artifactId>arquillian-junit-container</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.jboss.arquillian.protocol</groupId>
      <artifactId>arquillian-protocol-servlet</artifactId>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.jboss.arquillian.container</groupId>
      <artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
      <version>1.0.0.CR3</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.jboss.arquillian.extension</groupId>
      <artifactId>arquillian-persistence-dbunit</artifactId>
      <version>1.0.0.Alpha7</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.jboss.weld</groupId>
      <artifactId>weld-core</artifactId>
      <version>1.1.5.Final</version>
      <scope>test</scope>
    </dependency>

    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-simple</artifactId>
      <version>1.6.4</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.jboss.arquillian</groupId>
        <artifactId>arquillian-bom</artifactId>
        <version>1.1.8.Final</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
    </dependencies>
  </dependencyManagement>

Test 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="test" transaction-type="JTA">
    <provider>org.hibernate.ejb.HibernatePersistence</provider>
    <class>com.xyz.hellomaven.DummyEntity</class>

    <jta-data-source>java:jboss/datasources/ExampleDS</jta-data-source>
    <!--<jta-data-source>java:/DefaultDS</jta-data-source>-->
    <!--<jta-data-source>jdbc/arquillian</jta-data-source>-->

    <properties>
      <property name="hibernate.show_sql" value="true" />
      <property name="hibernate.hbm2ddl.auto" value="update"/>
      <!--<property name="hibernate.dialect" value="org.hibernate.dialect.H2Dialect" />-->
    </properties>
  </persistence-unit>
</persistence>

Test version

@RunWith(Arquillian.class)
public class GreeterTest {

  @Inject
  private Greeter instance; // Injection works!

  @PersistenceContext
  private EntityManager em; // Null pointer.

  public GreeterTest() {
  }

  @Deployment
  public static WebArchive createDeployment() {
    return ShrinkWrap.create(WebArchive.class)
        .addClasses(Greeter.class, PhraseBuilder.class, DummyInterceptor.class)
        .addAsResource("logging.properties", "META-INF/logging.properties")
        .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
        .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
  }

  @Test
  public void testCreateGreeting() {
    System.out.println("createGreeting");
    assertEquals("Hello, Steve!", instance.createGreeting("Steve"));
  }

  @Test
  public void testPersistence() {
    DummyEntity de = new DummyEntity();
    de.setId(1l);
    de.setName("Petr Cech");
    de.setAge(10);
    em.persist(de);

    Query q = em.createQuery("SELECT d.age FROM DummyEntity d");
    assertEquals(10, q.getResultList().get(0));
  }
}

The full Maven project is available on GitHub .

Please, what am I doing wrong?

+4
4

, - CI DI. , Mokito Weld,

<dependency>
      <groupId>org.jboss.arquillian.container</groupId>
      <artifactId>arquillian-weld-ee-embedded-1.1</artifactId>
      <version>1.0.0.CR3</version>
      <scope>test</scope>
</dependency>

, jboss (ExampleDS demo jboss h2) .

        <dependency>
            <groupId>org.jboss.as</groupId>
            <artifactId>jboss-as-arquillian-container-managed</artifactId>
            <version>7.1.1.Final</version>
            <scope>test</scope>
        </dependency>

. https://github.com/arquillian/arquillian-examples/blob/master/arquillian-persistence-tutorial/pom.xml

+1

@ , CDI , CDI.

:

wildfly , maven, maven, , wildfly , . ExampleDS , Wildfly .

. .

, Arquillian . , beans, ( session beans, Wildfly,...), CDI ( @Before @BeforeClass , , Junit), EntityManager EntityManagerFactory, , . CDI , , .

maven

<dependency>
    <groupId>org.jboss.weld.se</groupId>
    <artifactId>weld-se</artifactId>
    <version>2.1.2.Final</version>
    <scope>test</scope>
</dependency>

import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.*;

public class ExampleIT {

    private EntityManager em;

    protected static Weld weld;
    protected static WeldContainer container;

    @BeforeClass
    public static void init() {
        weld = new Weld();
        container = weld.initialize();
    }

    @AfterClass
    public static void close() {
        weld.shutdown();
    }

    @Before
    private void before(){
    em = Persistence.createEntityManagerFactory("MyPersistenceUnit").createEntityManager();
    }

    @Test
    public void testToto(){
        // Do something with entity manager ...
    }

}

, , Arquillian, .

+1

.

@PersistenceUnit
EntityManagerFactory emf; 

entityManager,

EntityManager em = emf.createEntityManager();

. , .

0

, + Entity Manger Factory. test-persistence.xml, Factory .

0

All Articles