Jackie
Jackie

Reputation: 23497

Junit not picking up persistance.xml EJB3

Here is my structure

\jpa
  \src
    \resources
      \META-INF
        \persistence.xml
  \oldtests
    \com
      TestClass.java
\war
  \oldtests
    \com
      TestSuite.java

TestClass extends BaseTest, and I am running it via the test suite.

In the test itself I am grabbing my persistance info with...

public class BaseTest extends TestCase {
  protected EntityManagerFactory emf;
  protected EntityManager em;
  @Override
  protected void setUp() throws Exception {
    super.setUp();
    emf = Persistence.createEntityManagerFactory("test");
    em = emf.createEntityManager();
    ...
  }
  ...
}

However, emf is always null, thus throwing an NPE on the next line. This leads me to believe my persistence.xml isn't getting picked up for some reason. Can someone help me solve where the file needs to be located? Is there a place I can confirm it isn't an issue with the file itself (bad configuration)?

I could add the persistance-unit on request but I doubt that is the issue since we are using this elsewhere. Also I am using eclipse to run my tests.

Upvotes: 0

Views: 106

Answers (2)

Jackie
Jackie

Reputation: 23497

The steps I needed to solve this are follows:

  1. Add IBM plugin jars to classpath

    I did this by creating a user library that points to <WASINSTALL>\plugins\*.jar

  2. Add items to your Junit Classpath.

    I did this by going to run > Debug Configurations

    • Next I selected the junit test suite configuration

    • Next I selected the classpath tab

    • Finally I added the JPA project and the user lib using the advanced option.

This seeemed to solve my issue

Upvotes: 1

Emil Sit
Emil Sit

Reputation: 23542

You need to add your src/resources directory to your Eclipse class path when you run the JUnit test. You may be able to do this at a project level, or just for your test runner.

Upvotes: 2

Related Questions