Junit In Spring Framework

Scenario: When you write web application with Spring MVC framework,
And you need to test your controllers with Junit Tests.
You have two approaches to proceed.

A)MockHttpServletRequest with Handler Adapter.
B)Hitting Controller methods directly.


To test Controller we need to create Mock HTTP request which will be handled by handler adapter and handler adapter will decide to which Controller’s method it should be passed to.

You will get MockHttpServletResponse object for response and then you can write assert statements on that response. For MockHttpServletRequest you dont need to have Server Up and running. You can say, Testing offline. You are generating mock requests and passing it to Controllers and then asserting response for it. So It will provide you end to end testing for Web Applications.

This technique is most suited for Web Application with REST calls.

For Writing Junit you generally needs 3 things.
1)Test Class
It contains actual Junit Tests

2)Test Application Context
It contains beans that required to create application context

3)Test Properties file
It can be text file with random data in key value pairs that Junit Test requires.

Test Class:
For test cases to run, we need to set up entire application context. For this add it before defining class

Setup Application Context


You can have xml file which contains definitions for beans.
You can refer it with annotation @ContextConfiguration()

Generally we put configuration files in resources, which comes under classpath.
so we write classpath:

Autowiring Controllers:
We don’t need to create instance of Controller class, but we can autowire singleton instance of bean of Controller class.

public UserPortalController controller;

private AnnotationMethodHandlerAdapter adapter;
private MockHttpServletRequest request;
private MockHttpServletResponse response;

Then you have set up method to initialize this all Objects.


	public void setUp() throws Exception {
		request = new MockHttpServletRequest();
		response = new MockHttpServletResponse();
		adapter = new AnnotationMethodHandlerAdapter();


Sample Test Case

               public void testGetApplications() {
               /* Specify REST API URL */
               /* Speficy HTTP Request Method */
               /* Specify parameters for request */
		request.addParameter("user-agent", "Mozilla");
		request.addParameter("client-ip", "");
               /* This is necessary if you are using Spring Version 3.1 */
		request.setAttribute(HandlerMapping.INTROSPECT_TYPE_LEVEL_MAPPING, true);
		try {
                        /* This method handles request, based on URL Mapping it is passed to controller method and response is populated in response object*/
                         adapter.handle(request, response, controller);
	                /*Assert Statements will come here*/		
		} catch (Exception e) {

To read values from properties file-
1.Add following property of context to your testappcontext.xml

context:property-placeholder location="classpath:testconfig.properties"

2.To read values from properties file

	private String variable;

One response to “Junit In Spring Framework

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s