Over a million developers have joined DZone.

JUnit Testing of Spring MVC Application: Testing Controller

· Java Zone

Discover how AppDynamics steps in to upgrade your performance game and prevent your enterprise from these top 10 Java performance problems, brought to you in partnership with AppDynamics.

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.




 

The Java Zone is brought to you in partnership with AppDynamics. AppDynamics helps you gain the fundamentals behind application performance, and implement best practices so you can proactively analyze and act on performance problems as they arise, and more specifically with your Java applications. Start a Free Trial.

Topics:

Published at DZone with permission of Krishna Prasad, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}