WebMvcTest при множественном TemplateResolver - PullRequest
0 голосов
/ 17 марта 2020

Я добавил второй TemplateResolver, потому что я хотел бы также использовать Thymeleaf для обслуживания шаблонов для простых текстовых шаблонов. При запуске приложения все работает, шаблоны Thymeleaf разрешаются.

Но теперь мои @WebMvcTests не работают. Я говорю, что:

Error resolving template [disclaimer/view], template might not exist or might not be accessible by any of the configured Template Resolvers 

TemplateResolver имеет следующие конфигурации:

@Configuration
public class SitemapViewResolverConfiguration {

    @Bean
    public ITemplateResolver thymeleafTextTemplateResolver() {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("/templates/sitemap/");
        templateResolver.setCacheable(false);
        templateResolver.setSuffix(".txt");
        templateResolver.setTemplateMode(TemplateMode.TEXT);
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setCheckExistence(TRUE);
        return templateResolver;
    }
}

@Configuration
public class PageViewResolverConfiguration {

    @Bean
    public ITemplateResolver thymeleafHtmlTemplateResolver() {
        ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
        templateResolver.setPrefix("classpath:/templates/view/");
        templateResolver.setCacheable(false);
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setCheckExistence(TRUE);
        return templateResolver;
     }
 }

Моя структура шаблона выглядит следующим образом: enter image description here

Тестовый класс:

@RunWith(SpringRunner.class)
@WebMvcTest(DisclaimerController.class)
public class DisclaimerControllerTest {
    @Autowired MockMvc            mvc;

    @Test
    public void testGet() throws Exception {

        mvc.perform(get("/disclaimer")
            .andExpect(status().isOk())
            .andExpect(view().name("disclaimer/view"))
            ;

}

}

Ошибка:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template [disclaimer/view], template might not exist or might not be accessible by any of the configured Template Resolvers

1 Ответ

0 голосов
/ 18 марта 2020

Хорошо, я нашел проблему. Поскольку я использую @WebMvcTest, он не загружал мой TemplateResolver. Как и раньше, я полагался на Spring Boot automati c resolver, сейчас я использую два самодельных распознавателя. Но они не обнаруживаются тестом, поэтому Mock Mvc не может разрешить его.

Я исправил это, добавив Configuration as Import:

@RunWith(SpringRunner.class)
@WebMvcTest({PageViewResolverConfiguration.class, DisclaimerController.class})
public class DisclaimerControllerTest {
    ...
}
...