Sunday, 15 September 2013

java - How to test jersey2 request filters? -



java - How to test jersey2 request filters? -

in jersey-2 application i'm using simple containerrequestfilter check basic authentication (probably reinventing wheel, bear me). filter goes this

@override public void filter(containerrequestcontext context) throws ioexception { string authheader = context.getheaderstring(httpheaders.authorization); if (stringutils.isblank(authheader)) { log.info("auth header missing."); context.abortwith(response.status(response.status.unauthorized) .type(mediatype.application_json) .entity(errorresponse.authenticationrequired()) .build()); } }

now i'd write test it, mocking 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 on eq(r) call, if looking @ string representation of response objects same. thought what's wrong?

java unit-testing jersey-2.0

No comments:

Post a Comment