JUnit testing of Spring MVC application: Testing Controller

In continuation of my earlier blogs on Introduction to Spring MVC and Testing Service layer in Spring MVC, in this blog I will demonstrate how to test Controller in Spring MVC. The objective of this demo is 2 fold, to build the Controller layer using TDD and increase the code coverage during JUnit testing of Controller.

For people in hurry, get the latest code from Github and run the below command

mvn clean test -Dtest=com.example.bookstore.web.controller.SpringMvcTestLoginControllerTest

Since in my earlier blog, we have already tested and implemented Service layer, in this blog we only need to focus on testing Controller. For testing the Controller I will use Spring-Test-MVC framework, which I have discussed enough in my earlier blogs. Spring-test-MVC implements a Domain Specific Language (DSL) for testing the Controller.

As a first step we define the LoginControllerTestConfiguration class with LoginControllerTest class. If you notice there are 2 beans defined in that class and we marked the as a @Configuration which shows that it is a Spring Context class. In the JUnit test we @Autowired LoginController class and @Autowired AccountService class. When creating the Bean in the configuration file we also stubbed the AccountService class using Mockito,

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
public class SpringMvcTestLoginControllerTest {

@Configuration
static class LoginControllerTestConfiguration {

@Bean
public AccountService accountService() {
return Mockito.mock(AccountService.class);
}

@Bean
public LoginController loginController() {
return new LoginController();
}
}
@Autowired
private LoginController loginController;

@Autowired
private AccountService accountService;
}

As a next step let us setup the data. If you notice the below code, we have also stubbed AccountService’s login method to return pre-configured data using Mockito.

@Before
public void setup() throws Exception {
this.account = new AccountBuilder() {
{
address("Herve", "4650", "Rue de la station", "1", null, "Belgium");
credentials("john", "secret");
name("John", "Doe");
}
}.build(true);

Mockito.when(this.accountService.login("john", "secret")).thenReturn(this.account);
}

Next we write the test where we set the loginController to the MockMvcBuilders and use Spring-test-MVC’s DSL to call the controller layer and do the asserts. Now if you run the test it will succeed.

@Test
public void testHandleLogin() throws Exception {
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(this.loginController).build();
mockMvc.perform(post("/login").param("username", "john").param("password", "secret"))
.andExpect(status().isOk())
.andExpect(request().sessionAttribute(LoginController.ACCOUNT_ATTRIBUTE, this.account))
.andExpect(redirectedUrl("/index.htm"));
}

Finally, loginController looks as below,

@Controller
@RequestMapping(value = "/login")
public class LoginController {

@Autowired
private AccountService accountService;

@RequestMapping(method = RequestMethod.POST)
public String handleLogin(@RequestParam String username, @RequestParam String password, HttpSession session)
throws AuthenticationException {
Account account = this.accountService.login(username, password);
session.setAttribute(ACCOUNT_ATTRIBUTE, account);
String url = (String) session.getAttribute(REQUESTED_URL);
session.removeAttribute(REQUESTED_URL); // Remove the attribute
if (StringUtils.hasText(url) && !url.contains("login")) { // Prevent loops for the login page.
return "redirect:" + url;
} else {
return "redirect:/index.htm";
}
}
}

I hope this blog helped you. In my next blog we will implement the web layer.

Reference:

Pro Spring MVC: With Web Flow by by Marten Deinum, Koen Serneels

About these ads

2 thoughts on “JUnit testing of Spring MVC application: Testing Controller

  1. Pingback: JUnit testing of Spring MVC application – Introduction | Krishna's Blog

  2. Pingback: JUnit testing of Spring MVC application: Testing Frontend using Selenium | Krishna's Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s