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< String> fixturesToLoad() { return new ArrayList< String>(); }

    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< String> names){
        List< String> 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< String> 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

Leave a Reply

Your email address will not be published. Required fields are marked

Information about Data protection

This site uses Akismet to reduce spam. Learn how your comment data is processed.

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}