-
Notifications
You must be signed in to change notification settings - Fork 3.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow in-memory unit testing of resources using views? #513
Comments
Yes, this would be helpful - I ended up running my tests with a similarly hacked copy of ViewMessageBodyWriter, which isn't a sustainable practice. Thanks for identifying this. |
In fact HttpHeaders can be injected into the ViewMessageBodyWriter, in a similar fashion to #651. Using the @ClassRule
public static final ResourceTestRule resources = ResourceTestRule.builder()
.addResource(new MemberNodeResource(backend, mockNode()))
.addProvider(new ViewMessageBodyWriter(new MetricRegistry()))
.addProvider(new ContextInjectableProvider<HttpHeaders>(HttpHeaders.class))
.build(); Note that I haven't setup a view renderer, so I will just get the plain html returned. Then to test: String response = resources.client().resource("/test/cancel").get(String.class);
org.assertj.core.api.Assertions.assertThat(response).contains("Test Cancelled"); |
If this #966 gets merged, then it should work. I verified.
|
Since
ViewMessageBodyWriter
contains@Context private HttpHeaders headers;
it's not possible to unit test resources using views with the lightweight in-memory Jersey provider (usingResourceTestRule
).However since a
MessageBodyWriter.writeTo
receives the request headers as aMultivaluedMap<String, Object>
argument there shouldn't be an absolute need for injectingHttpHeaders
.I did a very rough PoC by rewriting
ViewMessageBodyWriter.detectLocale()
into this horrid mess:I'd be happy to put some more effort into it and submit a PR if others think its valuable.
The text was updated successfully, but these errors were encountered: