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 :
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
Here is how the code could look like for the base class :
You can then code your tests like this :
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
@AfterClass annotations with
@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 :
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
drop scripts directly from Ebean. This is what is done in the
Once you’ve correctly made your tests work with the
FakeApplication, you might want to launch them directly from your IDE.
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,…
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 :
- Go to the ‘Run’ / ‘Edit Configurations’ menu
- Find the JUnit configuration under “Defaults”
- Fill the ‘VM Options’ field with :
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.
I hope that with these small tips and tricks, you will be able to enjoy even more discovering Play 2.0.