0

I try to perform some JUnit test in PlayFramework 2.3.8 I would like this test to be performed in the in memory database, so that I keep the "real" database untouched. This it what I've done:

public class SaveToolsTest extends WithApplication{

    @Before
public void startApp(){

    // Start the app in the in memory database. 
    Map<String, String> settings = new HashMap<String, String>();
    settings.put("db.default.url", "jdbc:h2:mem:play");
    settings.put("db.default.user", "");
    settings.put("db.default.password", "");
    app = Helpers.fakeApplication(settings);
    Helpers.start(app);
    }

    @Test
    public void saveUser(){     
         User user = createUser(); 

        // Save the user into DB. 
        Ebean.save(user);

        // Test if user != null
        assertNotNull(user); 
        assertThat(user.id).isNotNull(); 
    }

But by doing this, I connect the test to the actual db. How can I connect it to an in memory db?

Mornor
  • 3,471
  • 8
  • 31
  • 69

1 Answers1

1

In Play 2.3 and higher, you can specify the test application (and its settings like the used database) by overriding the provideFakeApplication() method. Play will call this method and start the application you have returned before running your tests.

import play.test.*;
import static play.test.Helpers.*;

public class MyTest extends WithApplication {

@Override
public FakeApplication provideFakeApplication(){
    return fakeApplication(inMemoryDatabase());
}

@Test
public void myTest(){
     //your tests here
}
ollie
  • 497
  • 4
  • 9