December 31, 2013

A generic helper for database related tests with play 2 and JPA

0  comments

All your database related unit tests for a play 2 java application have to handle the fakeApplication startup, load an fresh in memory database and load the test related fixtures. You need fixy and this helper to do the trick:
public class ModelTest {

    public FakeApplication app;
    public Fixy fixtures;
    EntityManager em = null;
    EntityTransaction tx = null;

    @Before
    public void beforeEachTest() {
        startFakeApp();
        loadFixtures();
        openTransaction();
    }

    @After
    public void afterEachTest(){
        closeTransaction();
        stopFakeApp();
    }

    // template method to load fixtures
    public List fixturesToLoad() { return new ArrayList(); }

    private void startFakeApp() {
        app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
        Helpers.start(app);
    }

    private void stopFakeApp(){
        Helpers.stop(app);
    }

    private void openTransaction() {
        em = JPA.em("default");
        JPA.bindForCurrentThread(em);
        tx = em.getTransaction();
        tx.begin();
    }

    private void closeTransaction() {
        if(tx != null) {
            if (tx.isActive()){
                if(tx.getRollbackOnly()) {
                    tx.rollback();
                } else {
                    tx.commit();
                }
            }

        }
        JPA.bindForCurrentThread(null);
        if(em != null) {
            em.close();
        }
    }

    private void loadFixtures() {
        openTransaction();
        fixtures = new JpaFixyBuilder(JPA.em()).build();
        fixtures.load(addPathAndPrefix(fixturesToLoad()));
        closeTransaction();
    }

    private static String[] addPathAndPrefix(List names){
        List fixtureNames = new ArrayList();
        for (String name : names){
            fixtureNames.add("fixtures/" + name + ".yaml");
        }
        return fixtureNames.toArray(new String[0]);
    }
}
To use the helper you just have to extend from ModelTest and override the fixturesToLoad method:
public class TaskTest extends ModelTest {

    @Override
    public List fixturesToLoad() {
        return Arrays.asList("tasks");
    }

    @Test
    public void testFindByName(){
        Task task = Task.findByName("Task 1");
        assertEquals("Task 1", task.name);
    }
}
The tasks.yaml loaded in fixturesToLoad must be placed in the conf/fixtures folder of the play application. The source code is pushed to Play4Jpa repo on github.

Tags

fixy, Java, JPA, ModelTest, Play Framework, Play4JPA, Unittest


You may also like

Blog url changed to https

I just changed the url of this blog to https://jensjaeger.com. TLS encryption is now the default for all request to this page. It might be possible that some image links on some articles are hard coded http. If you find such an error it would be nice if you leave me comment so i can

Read More

Format date and time in java with prettytime

Prettytime is a nice java library to format a java Date()s in a nice humanized ago format, like: moments ago 2 minutes ago 13 hours ago 7 months ago 2 years ago Prettytime is localized in over 30 languages. It’s super simple to use Add the dependency to your maven pom: org.ocpsoft.prettytime prettytime 3.2.7.Final or

Read More