I am using Resteasy serverside mock framework to test my service. I dont want to test business logic but I would like test the data produced by the service.
Using this approach I am able to create a simple test. However, in my RestEasy service I have a few dependency which I would like to mock.
See the following example service which I would like test. The collaborator must be mocked so the service can be tested.
@Path("v1")
Class ExampleService {
@inject
private Collaborator collaborator;
@GET
@Path("/")
@Produces({ "application/xml", "application/json" })
public Response getDummy() throws WSAccessException, JsonParseException, JsonMappingException, IOException {
...
Result result = collaborator.getResult();
..
return Response.ok("helloworld").build();
}
}
The junit test is the following
@Test
public void testfetchHistory() throws URISyntaxException {
Dispatcher dispatcher = MockDispatcherFactory.createDispatcher();
POJOResourceFactory noDefaults = new POJOResourceFactory(ExampleService.class);
dispatcher.getRegistry().addResourceFactory(noDefaults);
MockHttpRequest request = MockHttpRequest.get("v1/");
MockHttpResponse response = new MockHttpResponse();
dispatcher.invoke(request, response);
Assert.assertEquals(..);
}
How can I mock the collaborator in the test?