Enterprise Architect in HCL Technologies a $7Billion IT services organization. My role is to work as a Technology Partner for large enterprise customers providing them low cost opensource solutions around Java, Spring and vFabric stack. I am also working on various projects involving, Cloud base solution, Mobile application and Business Analytics around Spring and vFabric space. Over 23 yrs, I have build repository of technologies and tools I liked and used extensively in my day to day work. In this blog, I am putting all these best practices and tools so that it will help the people who visit my website. Krishna is a DZone MVB and is not an employee of DZone and has posted 64 posts at DZone. You can read more from them at their website. View Full User Profile

JUnit Testing of Spring MVC Application: Testing Controller

  • submit to reddit

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,

public class SpringMvcTestLoginControllerTest {

static class LoginControllerTestConfiguration {

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

public LoginController loginController() {
return new LoginController();
private LoginController loginController;

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.

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");

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.

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

Finally, loginController looks as below,

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

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.


Published at DZone with permission of Krishna Prasad, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)