springtester
The springtester
module is used in conjunction with the spring module. Internally, it uses the Mockito library and provides additional classes for injecting mocks/stubs when writing tests for your Play! application.
Getting started
Include springtester
in your Play! application by adding the following in your dependencies.yml
:
- play -> springtester [version]
As springtester
depends on the existing Spring Play! module, you will have to include it as well:
- play -> spring [version]
Note: springtester
has currently only been tested with version 1.0.1
of the spring
module.
Then update your project’s dependencies to include springtester
and its dependencies:
play deps
Usage
The springtester
module assumes you are wiring your application using the spring
module. Here is an example of two classes being wired up using the spring
module. The first is a Person
class that injects an AgeCalculator
and calls the calculate(...)
method:
@Component
public class Person {
// Here we are injecting the AgeCalculator into the Person class
@Resource private AgeCalculator ageCalculator;
public boolean canVote() {
int age = calculator.calculate("1-January-1980");
return age >= 18;
}
}
Note that person itself is annotated with the Component
annotation. The AgeCalculator
will then take birth date of the Person
and calculate their age:
@Component
public class AgeCalculator {
public int calculate(String birthDate) {
// some code that figures out the age of the person
}
}
Now let’s say you want to write a test for Person
called PersonTest
and wish to replace the AgeCalculator
with a mock that returns the age of 20 instead of calculating the actual age of the Person
:
public final class PersonTest extends SpringMockitoUnitTestCase {
@Subject private Person subject;
@Mock(name = "ageCalculator") AgeCalculator mockAgeCalculator;
@Test
public void testCanVote() throws Exception {
Mockito.when(mockAgeCalculator.calculate("1-January-1980")).thenReturn(20);
boolean actual = subject.canVote();
assertEquals(true, actual);
}
}
How it works:
- The
PersonTest
extendsSpringMockitoUnitTestCase
. This superclass does all the magic to inject mocks into your objects.
- The annotation
Subject
will auto-magically inject the subject into the test. In the example above, aPerson
object will be injected into the test. Be sure that the subject is a Spring component by annotating it with @Component.
- The annotation
Mock
(provided byMockito
) is used to auto-magically create a mock of the given class. In the example above, anAgeCalculator
object will be mocked and set against the fieldmockAgeCalculator
. TheMock
annotation can take thename
property which specifies the name of the field the mock will replace in the subject. If the field name of the object to be mocked is the same as the field name in the subject, you may omit the name property like so:
// The name of the mock "ageCalculator" has the same name as the
// field "ageCalculator" found in the Person class and therefore
// does not require the "name" property.
@Mock AgeCalculator ageCalculator;
- The remainder of the example test above uses
Mockito
to mock the call to thecalculate
method and return 20 and then asserts that thesubject.canVote()
call will return true (since 20 is older than or equal to 18).
- The class
SpringMockitoUnitTestCase
extendsDatabaseUnitTestCase
which provides the helper methodcheckCount(Class<? extends Model> cls, long expected)
which can be used to check the row count of table.
- The
DatabaseUnitTestCase
will also delete the test in-memory database before each test method to ensure a clean run for each test method execution.