How to check jersey2 query filters?

In my jersey-2 application, I use a very simple ContainerRequestFilter that will check basic authentication (maybe reinvent the wheel, but carry with me). The filter looks something like this:

 @Override public void filter(ContainerRequestContext context) throws IOException { String authHeader = context.getHeaderString(HttpHeaders.AUTHORIZATION); if (StringUtils.isBlank(authHeader)) { log.info("Auth header is missing."); context.abortWith(Response.status(Response.Status.UNAUTHORIZED) .type(MediaType.APPLICATION_JSON) .entity(ErrorResponse.authenticationRequired()) .build()); } } 

Now I would like to write a test for it, making fun of the ContainerRequestContext object.

 @Test public void emptyHeader() throws Exception { when(context.getHeaderString(HttpHeaders.AUTHORIZATION)).thenReturn(null); filter.filter(context); Response r = Response.status(Response.Status.UNAUTHORIZED) .type(MediaType.APPLICATION_JSON) .entity(ErrorResponse.authenticationRequired()) .build(); verify(context).abortWith(eq(r)); } 

This test fails when eq(r) called, even if you look at the string representation of Response objects, they are the same. Any idea what's wrong?

+5
source share
1 answer

I do not believe that you need the eq () method. You should check this context. abortWith (r). Maybe I missed something because you did not include what eq (r) is.

0
source

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


All Articles