[2.0.4.-java] How to write JPA Unit test

1,913 views
Skip to first unread message

Mathias Buchholz

unread,
Oct 24, 2012, 8:16:20 AM10/24/12
to play-fr...@googlegroups.com
Hello,

in my app i use JPA as persistence layer.
I also wrote some integration test calling my applicatin API for doing some stuff.

Now i want to write some Unit test for testing the database queries (without calling the API)


So i wrote an test, annotated it "@org.junit.Test" and "@play.db.jpa.Transactional"

I reveiced that exception:

                   No EntityManager bound to this thread. Try to annotate your action method with @play.db.jpa.Transactional

What code do i have to a add?
--> ?creating the JPA - config to database (what happens while calling the play API) ?
--> how binding EntityManager for testing?

Thanks

James Roper

unread,
Dec 4, 2012, 7:03:03 PM12/4/12
to play-fr...@googlegroups.com
Have you tried in Play 2.1?  It should work now, using the Helpers.route() call.

On Monday, 3 December 2012 18:01:21 UTC+11, Alex Zhang@GCXA wrote:
I have same issue, and waiting for someone's help

Mathias Buchholz

unread,
Jan 31, 2013, 6:44:03 AM1/31/13
to play-fr...@googlegroups.com
Hello James,

i have just read your post. I will try it with play-2.1 (I hope the debug mode is now faster than in RC1...)

Mathias Buchholz

unread,
Feb 7, 2013, 10:27:41 AM2/7/13
to play-fr...@googlegroups.com
Hi James,

it seems i need more input.

What is the use of Helpers.route?


    @Test
    public void testTest(){
       
        running(fakeApplication(), new Runnable() {
            @Override
            public void run() {
               
                Result result = Helpers.route(fakeRequest(Helpers.GET, "/login"));
               
                JPA.em().createNativeQuery("...").getResultList();
               
                assertThat(result).isNotNull();
                assertThat(status(result)).isEqualTo(OK);
                            
            }
        });
    }
.
.
.

No EntityManager bound to this thread. Try to annotate your action method with @play.db.jpa.Transactional

any hints? (just migrated to 2.1.0)
- Testing database queries without calling RestAPI
(testing the tiny "units")

by the way, it seems there some changes to unit tests. I cannot step-into code for debugging test code.
- using IDE eclipse. debugging is now fine again (play debug run)
- but debugging test code...(play debug test') NO

thanks,
Mathias



Am Mittwoch, 5. Dezember 2012 01:03:03 UTC+1 schrieb James Roper:

Patrick Pissang

unread,
Feb 17, 2013, 9:40:01 AM2/17/13
to play-fr...@googlegroups.com
Hi Mathias,

did you try this already? Works for me on Play 2.0

    @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();
    }
});
    }
});
    }

Best,
Patrick

Mathias Buchholz

unread,
Mar 1, 2013, 8:01:45 AM3/1/13
to play-fr...@googlegroups.com
Hello Patrick,

thanks for that. that runs great.

Meanwhile i found a solution for myself.
But i prefer yours (no need to load a driver).


    @Test()
    public void empty() {
running(fakeApplication(), new Runnable() {
    public void run() {

                    Class.forName("org.postgresql.Driver");
                    EntityManagerFactory emf = javax.persistence.Persistence
                            .createEntityManagerFactory("defaultPersistenceUnit");
                    EntityManager em = emf.createEntityManager();
                    JPA.bindForCurrentThread(em);
                    .
                    .
                    .
                    em.close();

                } catch (ClassNotFoundException e) {
                    play.Logger.error(e.getMessage());
                    e.printStackTrace();
                }
    }
});
    }


thanks,
Mathias

Matt Byrne

unread,
Jul 1, 2013, 9:57:27 PM7/1/13
to play-fr...@googlegroups.com
Thanks for the information guys - quite useful. 

Isn't Play meant to make things quicker and more concise though? How many levels of anonymous inner class do we need to do a simple test when Spring can do it with a couple of annotations.

Matt Byrne

unread,
Jul 2, 2013, 12:03:29 AM7/2/13
to play-fr...@googlegroups.com
There may be a more elegant solution out there but I found a solution on another post: http://grokbase.com/t/gg/play-framework/131mvapw39/2-0-4-functionaltest-error-no-entitymanager-bound-to-this-thread

I'm very new to Play! but was the best search result in a few hours of searching. Here's a sample base class for others that does not need anonymous inner classes per test.

package test;

import org.junit.After;
import org.junit.Before;
import play.db.jpa.JPA;
import play.db.jpa.JPAPlugin;
import play.test.FakeApplication;

import javax.persistence.EntityManager;

import static play.test.Helpers.fakeApplication;
import static play.test.Helpers.start;


public abstract class AbstractIntegrationTest {
    protected EntityManager em;

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

    @After
    public void tearDownIntegrationTest() {
        JPA.bindForCurrentThread(null);
        em.close();
    }
}

Any tests in base class can just do their thing with entity manager. 
Reply all
Reply to author
Forward
0 new messages