mercredi 18 novembre 2015

Running tests with EJB Container and JPA

I'm suffering in understanding how to properly set up tests in JUnit for EJB and JPA. I've seen advices telling to use Arquillian for the integration test, but I want to do a simple unit test.

What I have right now is a EJB with injects a Persintance Context. I run the application in Wildfly 9 and have the datasource configured there, so my persistance file looks like this:

    <jta-data-source>java:jboss/datasources/MyDataSource</jta-data-source>
    <properties>
        <!-- Properties for Hibernate -->
        <property name="hibernate.hbm2ddl.auto" value="create" />
        <property name="hibernate.show_sql" value="false" />
    </properties>

Now if I added glassfish-embedded-all to my test dependencies to run the embeded EJB container and created a simple test:

    Map<String, Object> properties = new HashMap<>();
    properties.put(EJBContainer.MODULES, new File("target/classes"));

    EJBContainer container = EJBContainer.createEJBContainer();
    TestEJb bean = (TestEJb) container.getContext().lookup("java:global/classes/TestEJb");
    assertNotNull(bean);
    assertEquals(1,bean.test());

Then I receive the error as the EJB contains the refence to the MyDataSource which is not known by the container. Should I create another persistance unit and pass it somehow to the EJB?

What is the right way to do it?

Aucun commentaire:

Enregistrer un commentaire