3

My application is based on playframework and contains multiple modules. The database interaction is handled trough JPA (<provider>org.eclipse.persistence.jpa.PersistenceProvider</provider>)

My task is to cover one of these modules with unit-tests.

Unfortunately running the "play test" command with unit tests provied on module-level results in the following Exception:

javax.persistence.PersistenceException: No Persistence provider for EntityManager named defaultPersistenceUnit

Persistence-Provider is defined globaly (outside of the Module) in conf/META-INF/persistence.xml copying the global persistence.xml to the module doesn't fix the issue.

Placing the tests outside of the module (in global test directory) and execute them works flawless presuming that there are no other tests within modules.

Can someone explain me why the Error comes up? Is there any way to have working JPA-capable tests on module level?

Thanks in advance

Urs

herry
  • 1,708
  • 3
  • 17
  • 30
uhon
  • 195
  • 1
  • 9
  • I think That exception you get because it doesn't specified any persistenceUnit. For JUnit project need to adjust another persistence.xml. The global persisitence.xml is use JTA? Can you describe for us your project structure? – herry Nov 11 '13 at 13:42
  • Project-structure lookl like this: [Structure](http://pastebin.com/YYRRvjQA). JPA is defined in global persitence.xml. How can i specify another persistence.xml for unit-tests? – uhon Nov 12 '13 at 08:48
  • thanks for structure. I think the global persistence.xml use JTA. Please confirm this! If the goal is test in module you need use same persistence context. Perhaps it is possible. But I don't know what running these test and your application is running before run these test. The my question is how declare EntityManger (or play famework's feature) in your JUnit test? Can share one example? – herry Nov 12 '13 at 11:16
  • hi herry, thanks for reply Here my [persistence.xml](http://pastebin.com/Vm1P6L2Q). Not sure if JTA is used. Tests look like this: [Abstract.java](http://pastebin.com/vkkqzQtk), [NavigationTest.java](http://pastebin.com/4cV06bF2) – uhon Nov 12 '13 at 13:13

2 Answers2

1

I had the same problem running JUnit tests from my play application in Eclipse.

To solve this issue you need to make the folder conf available to the all project.

Project Properties-> Java Build Path ->Source Add Folder and choose the conf folder.

Morlis
  • 11
  • 1
0

I checked your code. I think don't need another persistence.xml. Can you try these solutions in play module:

@Test()
public void empty() {
    running(fakeApplication(), new Runnable() {
     public void run() {
       JPA.withTransaction(new play.libs.F.Callback0() {
        public void invoke() {
         Object o = new Object(someAttrib);
         o.save();
         assertThat(o).isNotNull();
       }
     });
    }
  });
}

Or:

 @Before
    public void setUpIntegrationTest() {
        FakeApplication app = fakeApplication();
        start(app);
        em = app.getWrappedApplication().plugin(JPAPlugin.class).get().em("default");
        JPA.bindForCurrentThread(em);
    }

These codes are from this page. I don't test it!

Please modify your code to:

@BeforeClass
public static void setUp() {
    FakeApplication app = fakeApplication(inMemoryDatabase());
    start(app);

    em = app.getWrappedApplication().plugin(JPAPlugin.class).get().em("default");
    JPA.bindForCurrentThread(em);
}

Please try it!

herry
  • 1,708
  • 3
  • 17
  • 30
  • Hi herry. Thanks for your time. Unfortunately above samples don't work. Tried all of them. Still got stuck with the same PersistenceException – uhon Nov 13 '13 at 13:32
  • That is bad news! What is inMemoryDatabase() method? Is it initialize or add database connection? Can you try omit this parameter and try run application? It is only tip. May be you have simple problem: there is wrong adjust the persistence unit in your application. Please check [these adjustment](http://www.playframework.com/documentation/2.1-RC2/JavaJPA). – herry Nov 13 '13 at 15:04