TemplateEngineJson implements TemplateEngine { @Override public void invoke(Context context, Result result) { ResponseStreams responseStreams = context.finalizeHeaders(result); try (OutputStream outputStream = responseStreams.getOutputStream()) { Class<?> jsonView = result.getJsonView(); if (jsonView != null) { objectMapper.writerWithView(jsonView).writeValue(outputStream, result.getRenderable()); } else { objectMapper.writeValue(outputStream, result.getRenderable()); } } catch (IOException e) { logger.error("Error while rendering json", e); } } @Inject  TemplateEngineJson(ObjectMapper objectMapper); @Override void invoke(Context context, Result result); @Override String getContentType(); @Override String getSuffixOfTemplatingEngine();  }
@Test public void testJsonViewWorks() throws IOException { Mockito.<Class<?>>when(result.getJsonView()).thenReturn(View.Public.class); TemplateEngineJson jsonEngine = new TemplateEngineJson(objectMapper); jsonEngine.invoke(context, result); String json = new String(outputStream.toByteArray(), "UTF-8"); assertTrue(json.contains("field_one")); assertFalse(json.contains("field_two")); verify(context).finalizeHeaders(result); }