Mock Service inside a resource using a knit test frame

I have a resource for the leisure API that uses the service. This service has a constructor with parameters. I want to check this resource and make fun of this service. This Question: How to pass parameters to a REST resource using Jersey 2.5

didn't help because they used @Inject and I can't use it. Any suggestions?

Second question: how to pass a parameter to check this resource: My code:

@Path("/2/{subversion: [0-3]}/users") public class UserResource { Logger log = Logger.getLogger(UserResource.class); private MyService service; public void setService(Service ser) { this.service = ser; } 

@Context HttpServletRequest currentRequest;

 @GET @Produces("application/json") public Response getUsers(@Context HttpHeaders httpHeaders, @Context UriInfo uriInfo) { // my function } } 

How can I pass "httpHeaders" and "UriInfo". My test is as follows:

 Response response = target("/2/0/users/").request().get(); Users users = response.readEntity(Users.class); assertNotNull(users); 
+5
source share
1 answer

For a service, it is good practice to either enter through the constructor or the setter. This makes it easy to mock and pass during unit testing. Regarding ridicule, you should use a framework like Mockito . Then you can do something like

 MyService service = Mockito.mock(MyService.class); when(service.getObject()).thenReturn(new Object()); HttpHeaders headers = Mockito.mock(HttpHeaders.class); when(headers.getHeaderString("X-Header")).thenReturn("blah"); UriInfo uriInfo = Mockito.mock(UriInfo.class); when(uriInfo.getRequestUri()).thenReturn(URI.create("http://localhost")); 

Then you can simply pass all these mocks to your resource class when testing UNIT.

For INTEGRATION testing, you don’t need to scoff at the headers or uriinfo. Actual will be transferred. But you can still mock the service if you want. Here is an example

 public class MockServiceTest extends JerseyTest { public static interface Service { String getMessage(String name); } @Path("message") public static class MessageResource { private final Service service; public MessageResource(Service service) { this.service = service; } @GET public String get(@QueryParam("name") String name, @Context HttpHeaders headers, @Context UriInfo uriInfo) { String nameQuery = uriInfo.getQueryParameters().getFirst("name"); String header = headers.getHeaderString("X-Header"); assertNotNull(nameQuery); assertNotNull(header); return service.getMessage(name); } } private Service service; @Override public ResourceConfig configure() { service = Mockito.mock(Service.class); return new ResourceConfig().register(new MessageResource(service)); } @Test public void testIt() { Mockito.when(service.getMessage("peeskillet")).thenReturn("Hello peeskillet"); Response response = target("message").queryParam("name", "peeskillet").request() .header("X-Header", "blah") .get(); assertEquals(200, response.getStatus()); assertEquals("Hello peeskillet", response.readEntity(String.class)); } } 
+7
source

Source: https://habr.com/ru/post/1240963/


All Articles