Basic Ebean model testing
When you want to execute some tests that interact with your Models, you need to use a Play FakeApplication. This is described in the Java section of the Play 2.0 documentation.
Here is how the code looks like :
@Test
public void save() {
running(fakeApplication(), new Runnable() {
public void run() {
// Here is your real test code
Company company = new Company("My Company");
company.save();
assertThat(company.id).isNotNull();
}
});
}
As you can see, there is a lot of boilerplate code that we would like to avoid.
The easiest solution is to create a Test base class that will create de FakeApplication using a method annotated with @BeforeClass
Here is how the code could look like for the base class :
public class BaseModelTest {
public static FakeApplication app;
@BeforeClass
public static void startApp() {
app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
Helpers.start(app);
}
@AfterClass
public static void stopApp() {
Helpers.stop(app);
}
}
You can then code your tests like this :
public class CompanyTest extends BaseModelTest {
@Test
public void save() {
Company company = new Company("My Company");
company.save();
assertThat(company.id).isNotNull();
}
}
Database cleaning
To keep my tests really independent of each other, I systematically clean the database before each test.
One solution to achieve that is to create a new FakeApplication before each test. This can be done by replacing the @BeforeClass / @AfterClass annotations with @Before / @After and removing the static keyword in the methods’ declarations.
It will work well but unfortunately, this will have a bad impact on the execution time. It can be ok if you only have a few tests, but when the number of tests will grow, it can become problematic. It would be nice to find a more efficient solution.
To avoid creating a FakeApplication for each test, we could just execute directely some drop table and create table SQL scripts. This should be faster.
I found an easy way to do that by reusing the evolution script generated by the EbeanPlugin class. Here is how we can do this :
public class BaseModelTest {
public static FakeApplication app;
public static String createDdl = "";
public static String dropDdl = "";
@BeforeClass
public static void startApp() throws IOException {
app = Helpers.fakeApplication(Helpers.inMemoryDatabase());
Helpers.start(app);
// Reading the evolution file
String evolutionContent = FileUtils.readFileToString(
app.getWrappedApplication().getFile("conf/evolutions/default/1.sql"));
// Splitting the String to get Create & Drop DDL
String[] splittedEvolutionContent = evolutionContent.split("# --- !Ups");
String[] upsDowns = splittedEvolutionContent[1].split("# --- !Downs");
createDdl = upsDowns[0];
dropDdl = upsDowns[1];
}
@AfterClass
public static void stopApp() {
Helpers.stop(app);
}
@Before
public void createCleanDb() {
Ebean.execute(Ebean.createCallableSql(dropDdl));
Ebean.execute(Ebean.createCallableSql(createDdl));
}
}
Now before each test, you automatically have a database cleaned very quickly !
Here is a sample app illustrating this code : https://github.com/mguillermin/play2-ebean-testing
Note that if you have disabled evolutions, you will probably have to hack a little bit more to generate the create and drop scripts directly from Ebean. This is what is done in the EbeanPlugin.onStart() method.
IDE integration
Once you’ve correctly made your tests work with the FakeApplication, you might want to launch them directly from your IDE.
Why ?
Launching all your tests is easy with the command line. But when you want to launch a specific test class or method, it’s usually easier to use directly your IDE. You can then precisely select which tests to run, launch them in debug mode,…
The problem
If you try to do this on a test that use an Ebean entity, it will fail. Here is an example of what you will get :
java.lang.IllegalStateException: Class [class play.db.ebean.Model] is
enhanced and [class models.Company] is not - (you can not mix!!)
The problem is that your Model class is not enhanced by Ebean when you don’t launch your test from the Play console.
First solution : javaagent
Reading the Ebean reference guide , I found a way to automatically enhance your classes by using a specific javaagent.
All you have to do is to download Ebean, unzip it and then configure your IDE to add a JVM argument when launching the tests.
Here is how to do that with IntelliJ :

You can then enjoy running tests from your IDE.
Second solution : IDE plugin
If you use IntelliJ, there is an easier solution to enable enhancement automatically on your model classes. You will find in the plugin repository the Ebean weaver plugin that will do exactly what we want.
You just have to install it, restart, check the ‘Build’ / ‘Ebean weaving’ menu and that’s all.
Note that such a plugin exists also for Eclipse.
Conclusion
I hope that with these small tips and tricks, you will be able to enjoy even more discovering Play 2.0.