This post shows how to test for a view name in a Spring MVC application. Given a URI, the application returns a specific view name, and we want to test that. Any use-case for this? Well, we could think of a few, but more often than not, we want to make sure that a URI returns the same view name.
Requirements
Not many projects use Spring MVC nowadays. If some still do, this may come in handy.
- Spring Boot 2.1.1.RELEASE
- JDK 8
- IntelliJ IDEA
The codes in this post also work with the latest Spring Boot version and JDK 9 and above.
Spring MVC Controller That Returns A View Name
Okay, let’s start by creating a project in IntelliJ IDEA by including Spring Web and Spring MVC dependencies. Then, we make a class with @Controller annotation. Next, we create a public method that maps for an HTTP GET request on / URI. The URI returns the “index” view name. This view name refers to an index.html in our project. Consider the following codes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | package com.turreta.springmvc.springmvc.controllers; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class IndexController { @RequestMapping("/") public String index() { return "index"; } } |
Our View In Our Spring MVC Project
So, we have a view name, and it refers to an HTML that has the following content. It does not contain much and only displays a “Hello World” on a white page. There are not even JavaScript codes or placeholders.
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!doctype html> <html> <head lang="en" > <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Index Page</title> </head> <body> <h1>Hello World</h1> </body> </html> |
Test For The View Name
Next, we create a unit test. It uses MockMvc. In line 27, we access the “/” URI, and we are expecting an HTTP OK and the “index” view name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | package com.turreta.springmvc.springmvc.controllers; import org.junit.Before; import org.junit.Test; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.setup.MockMvcBuilders; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; public class IndexControllerTest { private MockMvc mockMvc; private IndexController indexController; @Before public void setUp() { indexController = new IndexController(); mockMvc = MockMvcBuilders.standaloneSetup(indexController).build(); } @Test public void index() throws Exception { mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("index")); } } |
The solution may be no longer relevant nowadays because most new applications use REST controllers that work with JSON data. However, we can use the same principle for testing REST API and not expecting the response as a view name for an HTML file.
And that’s how we can test for view name in a Spring MVC application.