Writing Junit Tests with Spring4 Mvc

By Dhiraj Ray, 19 November,2016  

Today we will take a look into writing Junit test cases with Spring Mvc. We will be creating a sample Spring MVC app with hibernate and write few test cases using Junit and clear our concepts on writing Junit test cases with Spring Mvc.


JUnit is a unit testing framework for the Java. It is mostly used in test-driven development, and is one of a family of unit testing frameworks which is collectively known as xUnit. It is always recommended to write test cases for whatever functionality a developer develops irrespective of simple applications or larger ones. The larger projects which has multiple modules and dependencies, it is not really possible to perform manual end-to-end functionality test on the go. Junit is very helpful in those scenarios. Also Junit encourages for modular programming as modular programming can be easily tested using Junit.

To start with writing Junit test cases first we need to identify what are the different units of the app we are developing. In a typical Spring MVC app, Controllers, Services, DAOs each represent a single unit. And our Junit test cases should represent these each units seperately. In those cases of course we also need Mockito fameworks to mock different units while writing test caases for a particular unit.

Here in this posts, we are intrested to write Junit test cases to test end-to-end functionality as our application will be a simple Spring MVC application. Here is the spring web application for which we will be writing the Junit test cases. Spring framework has made writing Junit test cases much simpler as it has many built-in supports to write Junit test cases for a Spring web app.

Environment Setup

1. JDK 8 2. Spring 4 3. Hibernate 4 4. Intellij Idea/ eclipse

Project Structure


Maven Dependencies

Following are the maven dependencies which needs to be included in the pom file to start with the Junit test cases.

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.1.2</version> <scope>test</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring.version}</version> <scope>test</scope> </dependency> <dependency> <groupId>org.hamcrest</groupId> <artifactId>hamcrest-all</artifactId> <version>1.3</version> <scope>test</scope> </dependency>

Java Config

Following is the configuration required to integrate Junit with Spring mvc.

package com.developerstack.test.config; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; @ComponentScan(basePackages = "com.developerstack") public class TestBeanConfig { }

Server Side

Now let us define our controller. This controller will map the requests coming from angular service. Here we have 2 mappings to validate the user credentials and to load the user details.

package com.developerstack.controller; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import com.developerstack.model.UserDetails; import com.developerstack.service.UserService; import com.google.gson.Gson; @Controller public class UserController { @Autowired private UserService userService; @RequestMapping(value = "/validate", method = RequestMethod.POST) public ResponseEntity validate(@RequestBody String userString, HttpServletRequest request) { Map user = new Gson().fromJson(userString, Map.class); userService.validateUser(user, request); return new ResponseEntity(HttpStatus.OK); } @RequestMapping(value = "/details", method = RequestMethod.GET) public ResponseEntity> userDetails() { List userDetails = userService.getUserDetails(); return new ResponseEntity>(userDetails, HttpStatus.OK); } }

Following is the entity class. The class is annotated as hibernate entity.

package com.developerstack.model; @Entity @Table public class UserDetails { @Id @Column @GeneratedValue(strategy = GenerationType.IDENTITY) private int id; @Column private String firstName; @Column private String lastName; @Column private String email; @Column private String password; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }

Let us define our service. It has simple logic to validate the user from DB and return the userdetails.

package com.developerstack.service.impl; import java.util.Arrays; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.springframework.stereotype.Service; import com.developerstack.model.UserDetails; import com.developerstack.service.UserService; @Service public class UserServiceImpl implements UserService { @Autowired private UserDao userDao; public void validateUser(Map user, HttpServletRequest request) { String userName = user.get("email"); String password = user.get("password"); UserDetails userDetails = userDao.findUserByEmail(email); if(userDetails == null || !userDetails.getPassword().equals(password)) { throw new RuntimeException("Invalid credentials."); } return userDetails; } } public List getUserDetails() { return userDao.getUserDetails(); } }

Let us define the dao.

package com.developerstack.dao.impl; import java.util.List; import org.hibernate.Criteria; import org.hibernate.SessionFactory; import org.hibernate.criterion.Restrictions; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Component; import com.developerstack.dao.UserDao; import com.developerstack.model.UserDetails; @Component public class UserDaoImpl implements UserDao { @Autowired private SessionFactory sessionFactory; public UserDetails findUserByEmail(String email) { UserDetails userDetails = null; Criteria criteria = sessionFactory.openSession().createCriteria(UserDetails.class); criteria.add(Restrictions.eq("email", email)); List entityList = criteria.list(); if(!entityList.isEmpty()) { userDetails = entityList.get(0); } return userDetails; } public List getUserDetails() { Criteria criteria = sessionFactory.openSession().createCriteria(UserDetails.class); return criteria.list(); } }

Let us define our test class now. @WebAppConfiguration :Typically a Servlet container would provide the ServletContext but since we are in a testing environment, you need a fake servletContext and @WebAppConfiguration provides that.

package com.developerstack.controller; import com.developerstack.model.UserDetails; import com.developerstack.test.config.TestBeanConfig; import com.google.gson.Gson; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; import java.util.HashMap; import java.util.List; import java.util.Map; import static org.junit.Assert.*; @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(classes = {TestBeanConfig.class}) public class UserControllerTest { @Autowired private UserController userController; @Test public void validateUser_Test_Positive() { Map params = new HashMap(); params.put("email", "admin@admin.com"); params.put("password","admin"); ResponseEntity result = userController.validate(new Gson().toJson(params)); assertNotNull(result); assertEquals(result.getStatusCode(), HttpStatus.OK); } @Test public void userDetails_Test() { ResponseEntity> result = userController.userDetails(); assertNotNull(result); assertTrue(result.getBody().size() > 0); } }


I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.

Download the source

References: Spring Framework References Spring Boot References

Suggest more topics in suggestion section or write your own article and share with your colleagues.

Is this page helpful to you? Please give us your feedback below. We would love to hear your thoughts on these articles, it will help us improve further our learning process.

Further Reading: