VelocityMailTemplateService { public String mergeTemplate(@NotNull String templatePath, Map<String, Object> contextMap) { var velocityTemplate = velocityEngine.getTemplate(templatePath); var velocityContext = new VelocityContext(new HashMap<>(contextMap)); var writer = new StringWriter(); velocityTemplate.merge(velocityContext, writer); return writer.toString(); } VelocityMailTemplateService(VelocityEngine velocityEngine); String mergeTemplate(@NotNull String templatePath, Map<String, Object> contextMap); Pair<String, String> getSubjectAndText(@NotNull String templatePath, Map<String, Object> contextMap);  }
@Test public void mergeContextMergesExpectedTemplateAndContext() { var templatePath = "TestTemplate.vm"; var contextMap = Map.<String, Object>of("key1", "value1", "key2", 2L); var expectedResult = "Nice context merge!"; given(velocityEngineMock.getTemplate(templatePath)).willReturn(velocityTemplateMock); doAnswer(invocation -> { var actualWriter = (StringWriter) invocation.getArgument(1); actualWriter.append(expectedResult); return null; }).when(velocityTemplateMock).merge( argThat(new VelocityContextArgumentMatcher(contextMap)), argThat(new StringWriterArgumentMatcher("")) ); var actualResult = velocityMailTemplateService.mergeTemplate(templatePath, contextMap); assertThat(actualResult).isEqualTo(expectedResult); }