I am trying to mock UriInfo object to pass to a restful service method , the response should be List if successul request with the correct QueryParams, and StatusResponse in case of any error or failure happened, I am sending a correct request but the controller receives it as If I sent a wrong request with no QueryParams , and gives me a Status Response Object.
RestController.java
Service method:
public Response getObjectives(@Context UriInfo info) {
try {
return Response.ok(objectiveService.getObjectives(info.getQueryParameters())).build();
} catch (Exception e) {
return Response.status(Status.INTERNAL_SERVER_ERROR).entity(StatusResponse.EXPIRED_SESSION).build();
}
}
RestControllerTest.java
private UriInfo mockUriInfo(String uri) throws URISyntaxException {
UriInfo uriInfo = Mockito.mock(UriInfo.class);
Mockito.when(uriInfo.getRequestUri()).thenReturn(new URI(uri));
return uriInfo;
}
@Test
@DataSet({ "/datasets/object_target_type.xml", "/datasets/st_objective_levels.xml", "/datasets/st_objectives.xml", "/datasets/st_related_objectives.xml" })
@Transactional(TransactionMode.ROLLBACK)
public void testGetObjectives() throws Exception {
UriInfo uriInfo = mockUriInfo("http://localhost:8080/v1.0/objectives?planId=987&level=2");
Response response = restController.getObjectives(uriInfo);
Object objs = response.getEntity();
ReflectionAssert.assertLenientEquals(objs, Arrays.asList(
new Objective(2L, "Obj1", new Level(2L), 987L, new TargetType(1L), 222d, 3),
new Objective(3L, "Obj2", new Level(2L), 987L, new TargetType(1L), 333d, 3),
new Objective(5L, "Obj5", new Level(2L), 987L, new TargetType(1L), 555d, 3),
new Objective(6L, "Obj6", new Level(2L), 654L, new TargetType(1L), 666d, 3))
);
}
But the actual response is a StatusResponse as if an exception was thrown in the RestController method.
I think it failed to mock the UriInfo and it doesn't see the QueryParams.
Help me, whats wrong with mocking the UriInfo object
Aucun commentaire:
Enregistrer un commentaire