Community contributed extensions

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.

Changes

Version 2.1:

Version 2.0:

Version 1.6:

Version 1.1:

Version 1.0:

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]

Then update your project’s dependencies to include springtester and its dependencies:

play deps

Usage

Standard Mocks

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 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;

Partial Mocks

Similar to the annotation Mock, you can also annotate using the annotation PartialMock. This annotation exposes the partial mocking (i.e. spy) feature from Mockito. In the example below, we are stubbing a call to sport(). Any calls to number() will call the “real” code.

public final class PersonTest extends SpringMockitoUnitTestCase {
    @Subject private Person subject;
    @PartialMock(name = "favouriteThings") FavouriteThings partialMockFavouriteThings;
    @Test
    public void testSayFavouriteThings() throws Exception {
        // Assuming sport() would normally return "football", the follow statement will ensure that "tennis" is returned instead.
        // All other methods found on the partialMockFavouriteThings object will be called as normal (i.e. not stubbed out).
        Mockito.when(partialMockFavouriteThings.sport()).thenReturn("tennis");
        ...
    }
}

Note: Be sure to read the gotcha's on the official Mockito site when spying on real objects (partial mocks)

Functional Testing

public class FunctionalExampleTest extends SpringMockitoFunctionalTestCase {
    @Mock(name = "ageCalculator") AgeCalculator mockAgeCalculator;
    @Test
    public void testSomething() {
        // Do some mockito expectations
        // This URL will hit a controller that will eventually inject the mocked AgeCalculator
        Http.Response response = GET("/some/url");
        // Verify response and expectations
    }
}
@Test
public void testSomeTestMethod() {
    // Create and register the mock object for just this test method
    AgeCalculator mockAgeCalculator = Mockito.mock(AgeCalculator.class);
    registerObject("ageCalculator", mockAgeCalculator);
    // Do some mockito expectations
    // Make your calls for testing
    // Verify response and expectations
}
public class ExampleTest extends SpringMockitoUnitTestCase {
    @Resource private SomeObject someObject;
    @Test
    public void testSomething() {
        someObject.someMethod(...);
        ...
    }
}

Source Code

If you wish to play with the source, please download the code here

To set up your project for IntelliJ just run these commands in the root springtester directory:

Read the README in the samples-and-tests directory to learn how to run the example application.