Manu Zi Manu Zi - 4 years ago 342
Java Question

Spring junit test template might not exist

First of all, the application is successfully compiling and running.

I try to unit test my controllers in spring boot. my test-controller looks like:

@RunWith(SpringRunner.class)
@ContextConfiguration(classes = { Application.class })
@Rollback
@Transactional
@WebMvcTest(PasswordResetController.class)
public class PasswordResetControllerTest {
@Autowired
private MockMvc mvc;

@Test
public void getPasswordForgetView() throws Exception {
this.mvc.perform(get("/password/forget"))
.andExpect(status().isOk());
}
}


with this i get the following exception:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Error resolving template "password-forget-html", template might not exist or might not be accessible by any of the configured Template Resolvers


if I debug the configuration, I see that it goes trough my
WebMvcConfig.class
in there I point the templates to the
/WEB-INF/templates/
folder:

@Configuration
@ComponentScan(basePackages = { "com.tmp" })
public class WebMvcConfig extends WebMvcConfigurerAdapter {

@Value("${mail.templates.cacheable}")
private boolean mailTemplatesCacheable;

@Value("${html.templates.cacheable}")
private boolean htmlTemplatesCacheable;

public static final String URL_RESOURCES = "/modules/**";
public static final String PATH_RESOURCES = "/modules/";

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(URL_RESOURCES).addResourceLocations(PATH_RESOURCES);
}

@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/login").setViewName("login");
}

@Bean
public TemplateResolver templateResolver() {
SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
templateResolver.setPrefix("/WEB-INF/templates/");
templateResolver.setSuffix(".html");
templateResolver.setTemplateMode("HTML5");
templateResolver.setCharacterEncoding("UTF-8");
templateResolver.setCacheable(htmlTemplatesCacheable);
return templateResolver;
}

@Bean
public TemplateResolver mailResolver() {
SpringResourceTemplateResolver result = new SpringResourceTemplateResolver();
result.setPrefix("/WEB-INF/mail/");
result.setSuffix(".html");
result.setTemplateMode("HTML5");
result.setCacheable(mailTemplatesCacheable);
result.setOrder(2);
result.setCharacterEncoding("UTF-8");
return result;
}

@Bean
public SpringTemplateEngine templateEngine() {
SpringTemplateEngine templateEngine = new SpringTemplateEngine();
templateEngine.addTemplateResolver(templateResolver());
templateEngine.addTemplateResolver(mailResolver());
templateEngine.addDialect("java8time", java8TimeDialect());
templateEngine.addDialect("sec", springSecurityDialect());
templateEngine.addDialect("layout", thymeleafLayoutDialect());
return templateEngine;
}

@Bean
public ThymeleafViewResolver viewResolver() {
ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
thymeleafViewResolver.setTemplateEngine(templateEngine());
thymeleafViewResolver.setCharacterEncoding("UTF-8");
thymeleafViewResolver.setCache(false);
return thymeleafViewResolver;
}

// Dialect Beans
...
}


And yes, there are templates in the folder.

Any suggestions? Thank's in advance

Answer Source

from the comment from @eis I got the solution. the annotations WebMvcTest and ContextConfiguration are contradicting each other. I change the class to the following:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { Application.class })
public class PasswordResetControllerTest {

    @Autowired 
    private WebApplicationContext applicationContext;

    private MockMvc mockMvc;

    ...

    @Before  
    public void init() {  
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.applicationContext).build();
    }
}

now it's work like a charm

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download