【问题标题】:Thymeleaf 3 with Spring Boot 2, unable to process templateThymeleaf 3 与 Spring Boot 2,无法处理模板
【发布时间】:2018-11-15 00:11:46
【问题描述】:

在阅读之前,请注意我是 Thymeleaf、Spring 和 Mockito 的新手。所以我期待我犯了一些业余错误。

我正在编写代码以使用 thymeleaf HTML 模板发送电子邮件。我一直在网上查看不同的教程并尝试设置所有内容。我认为我的设置很好,但是当我编写测试以检查模板是否正在处理时,我得到的是“null”而不是某种形式的字符串。

我在 src/main/resources/templates 和 src/test/resources/templates 中都放置了一个模板文件

文件名为email.html

以下是我配置模板引擎和解析器的代码。

    @Configuration
    public class SpringMailConfig{
...
    @Bean
        public SpringTemplateEngine springTemplateEngine(){
            SpringTemplateEngine templateEngine = new SpringTemplateEngine();
            templateEngine.addTemplateResolver(htmlTemplateResolver());
            templateEngine.setTemplateEngineMessageSource(emailMessageSource());
            return templateEngine;
        }

        @Bean
        public SpringResourceTemplateResolver htmlTemplateResolver() {
            SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
            templateResolver.setPrefix("/templates/");
            templateResolver.setSuffix(".html");
            templateResolver.setTemplateMode(TemplateMode.HTML);
            templateResolver.setCharacterEncoding(StandardCharsets.UTF_8.name());
            return templateResolver;
        }
...
    }

这是我要处理模板的类,我只展示我认为对这个问题很重要的内容。

@Component
public class EmailServiceHelper {

    @Autowired
    public SpringTemplateEngine springTemplateEngine;

   private  Context prepareContext(Locale locale, Map<String, Object> contextMap){
        final Context context = new Context(locale);
        context.setVariables(contextMap);
        return context;
    }

    //returns as a string the template with the custom values inserted
    private String returnHtmlContent(String templatePath, Locale locale, Map<String,Object> map){
        Context ctx = prepareContext(locale, map);
        return springTemplateEngine.process(templatePath, ctx);
    }

}

这是我得到错误的测试类(它的一部分), springTemplateEngine.process(templatePath, ctx) 返回 null。

@RunWith(MockitoJUnitRunner.class )
public class EmailServiceHelperTest {
@Mock
    SpringTemplateEngine springTemplateEngine;

    @Mock
    SpringResourceTemplateResolver springResourceTemplateResolver;
@InjectMocks
EmailServiceHelper helper;

   @Before
    public void setup(){

        MockitoAnnotations.initMocks(this);
        helper = new EmailServiceHelper();
        helper.springTemplateEngine = springTemplateEngine;
    helper.springTemplateEngine.setTemplateResolver(springResourceTemplateResolver);
    }

@Test
    public void testTemplateMessageHasContent(){
        try {
            Locale locale = new Locale("en");
            Map<String, Object> contextMap = new HashMap<>();
            contextMap.put("name", "Test name");
            MimeMessage message = helper.prepareMimeMessage(mail, mailSender, contextMap, "email", locale);
            assertNotNull(message.getContent());
        }catch (MessagingException | IOException e){
            e.printStackTrace();
            fail("Testing if template message has content failed!");
        }
    }
}

这就是我的电子邮件模板的样子。

<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>TEMPLATE</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body>
<h1 th:text="${name}">Hello, Static Person!</h1>

</body>
</html>

【问题讨论】:

  • 你能发布你的 pom.xml 吗?
  • 您的电子邮件模板保存在哪个文件夹中?

标签: spring spring-boot mocking mockito thymeleaf


【解决方案1】:

我想通了。怀疑这是我的菜鸟错误。因为我在模拟模板引擎,所以我必须在我的测试中创建处理模板的方法。然后我做了:

when(mockObject.processTemplate()).thenReturn(localProcessTemplateMethod();

【讨论】:

  • 你能提供更多关于解决方案的细节吗,我和你的问题完全一样。
猜你喜欢
  • 2017-03-08
  • 2019-04-02
  • 2016-01-26
  • 1970-01-01
  • 2018-02-18
  • 2019-10-11
  • 2018-11-13
  • 2019-10-31
  • 2020-05-26
相关资源
最近更新 更多