Labs ICT
Pro Login

Testing Templates

Unit testing Thymeleaf templates.

Why Test Your Templates?

Templates aren't just static HTML — they contain logic, loops, and conditionals. Testing them ensures your UI renders correctly with different data sets. It's the same principle as unit testing your Java code, but for your views.


# pom.xml
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-test</artifactId>
    <scope>test</scope>
</dependency>
    

Spring Boot's test starter includes everything you need. MockMvc, AssertJ, and Hamcrest are all ready to go.

Using MockMvc

MockMvc is the simplest way to test your templates. It simulates HTTP requests without starting a real server, so your tests run fast.


@RunWith(SpringRunner.class)
@SpringBootTest
public class TemplateTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void homePageRenders() throws Exception {
        mockMvc.perform(get("/"))
               .andExpect(status().isOk())
               .andExpect(view().name("home"))
               .andExpect(content().string(containsString("Welcome")));
    }
}
    

This test hits the / endpoint, checks that it returns a 200 status, uses the home template, and contains the word "Welcome" in the output.

Standalone Setup

If you want to test a template without the full Spring context, use standaloneSetup. It's lightweight and perfect for quick template checks.


@RunWith(SpringRunner.class)
@WebMvcTest(HomeController.class)
public class HomeTemplateTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void shouldShowUserName() throws Exception {
        mockMvc.perform(get("/profile")
               .param("name", "Alice"))
               .andExpect(status().isOk())
               .andExpect(content().string(containsString("Alice")));
    }
}
    

This only loads the specified controller and its view resolution. It's much faster than a full context load and great for focused template tests.

Try it Yourself →

WebApplicationContext Setup

For tests that need the full Spring context — say, because your template depends on services and repositories — use webApplicationContextSetup.


@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class FullTemplateTest {

    @Autowired
    private WebApplicationContext context;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders
            .webAppContextSetup(context)
            .build();
    }

    @Test
    public void blogPageShowsPosts() throws Exception {
        mockMvc.perform(get("/blog"))
               .andExpect(status().isOk())
               .andExpect(content().string(containsString("post-title")));
    }
}
    

This loads everything — your services, repositories, and all configurations. Use it when your template logic depends on real database data or complex service interactions.

Asserting Rendered Content

Once your template renders, you can assert on specific elements, attributes, or text content. This catches issues like missing variables or broken conditionals.


@Test
public void dashboardShowsStats() throws Exception {
    mockMvc.perform(get("/dashboard"))
           .andExpect(status().isOk())
           .andExpect(model().attributeExists("stats"))
           .andExpect(content().string(containsString("Total Users")))
           .andExpect(content().string(containsString("Active Sessions")));
}
    

Combining model assertions with content checks gives you solid coverage. You verify both that the right data was passed and that the template actually used it.