In an app based on the Play! Framework (2.0, Java) I want to mock a third party API when testing the controllers. I’ve picked Mockito for this because I couldn’t find out about any built-in mocking features in Play!
I have something like this:
@Test
public void someTest() {
ThirdParty thirdParty = mock(ThirdParty.class);
when(thirdParty.someUnwantedMethod()).thenReturn("foo");
running(fakeApplication(), new Runnable() {
public void run() {
Result result = callAction(controllers.routes.ref.MyController.doImportantStuff());
verify(thirdParty.someUnwantedMethod()); // Verify that method in mock/API is called
assertThat(contentAsString(result)).contains("foo");
}
});
}
(the controller in turn calls “someUnwantedMethod()” on an instance of the ThirdParty class which should use the mock instead when testing)
How do I get my controller to pick up the mock?
There is nothing play-specific about this