ThymeleafTemplateEngine extends TemplateEngine<ThymeleafTemplateEngine> { @Override public HttpResponse render(String name, Object... keyOrVals) { TemplatedHttpResponse response = TemplatedHttpResponse.create(name, keyOrVals); response.setBody(new LazyRenderInputStream(() -> { Context ctx = new Context(Locale.US, response.getContext()); try { return new ByteArrayInputStream(thymeleafEngine.process(name, ctx).getBytes(encoding)); } catch (UnsupportedEncodingException e) { throw new MisconfigurationException("core.UNSUPPORTED_ENCODING", encoding, e); } })); HttpResponseUtils.contentType(response, "text/html"); return response; }  @Override HttpResponse render(String name, Object... keyOrVals); @Override Object createFunction(Function<List, Object> func); void setDialects(Set<IDialect> dialects); void setTemplateResolvers(Set<ITemplateResolver> templateResolvers); void setMessageResolvers(Set<IMessageResolver> messageResolvers); void setLinkBuilders(Set<ILinkBuilder> linkBuilders); void setCacheManager(ICacheManager cacheManager); void setPrefix(String prefix); void setSuffix(String suffix); void setEncoding(String encoding);  }
@Test public void test() { HttpResponse response = engine.render("test1", "name", "kawasima", "message", "hello"); try (BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) response.getBody()))) { assertTrue(reader.lines().anyMatch(s -> s.contains("<span>kawasima</span>"))); } catch (IOException e) { fail("IOException occurred"); } }
@Test public void test2() { Locale.setDefault(Locale.ENGLISH); HttpResponse response = engine.render("test1", "name", "kawasima", "message", "hello"); System.out.println(response.getBodyAsString()); }
@SuppressWarnings("unchecked") @Test public void customFunction() { HttpResponse response = engine.render("test2", "func", (Function<List, Object>) list -> list.stream() .filter(Objects::nonNull) .map(Object::toString) .collect(Collectors.joining("&"))); try (BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) response.getBody()))) { String result = reader.lines().collect(Collectors.joining("\n")); assertTrue(result.contains("<span>abc&amp;def</span>")); } catch (IOException e) { fail("IOException occurred"); } }