VelocityMailTemplateService { public Pair<String, String> getSubjectAndText(@NotNull String templatePath, Map<String, Object> contextMap) { var unparsed = mergeTemplate(templatePath, contextMap); var subject = getSubStringBetweenDelimiters(unparsed, SUBJECT_DELIMITER, TEXT_DELIMITER); var text = getSubStringBetweenDelimiters(unparsed, TEXT_DELIMITER, null); return new Pair<>(subject, text); } 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 getSubjectAndTextReturnsExpectedSubjectAndText() { var expectedSubject = "Cool subject!"; var expectedText = "Even cooler text!"; var expectedResult = new Pair<>(expectedSubject, expectedText); var templatePath = "TestTemplate.vm"; var templateContent = String.format("%s\r\n%s\r\n%s\r\n%s", VelocityMailTemplateService.SUBJECT_DELIMITER, expectedSubject, VelocityMailTemplateService.TEXT_DELIMITER, expectedText); var contextMap = Map.<String, Object>of("key", "value"); given(velocityEngineMock.getTemplate(templatePath)).willReturn(velocityTemplateMock); doAnswer(invocation -> { var actualWriter = (StringWriter) invocation.getArgument(1); actualWriter.append(templateContent); return null; }).when(velocityTemplateMock).merge( argThat(new VelocityContextArgumentMatcher(contextMap)), argThat(new StringWriterArgumentMatcher("")) ); var actualResult = velocityMailTemplateService.getSubjectAndText(templatePath, contextMap); assertThat(actualResult).isEqualTo(expectedResult); }