Pages

Sunday, June 16, 2013

Testing Spring MVC application - Spring 3

Testing Spring MVC application is always bit tricky because the test support is not that great for MVC. This weekend when I was searching for options to test, I stumbled across this spring-test-mvc framework which is now integrated with Spring-test module from 3.2.3.Release version on wards.

I tested it using a quick example and it looks impressive to start with. Lets see how to use this to test your application.

Add this dependency in your POM file

<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.2.3.RELEASE</version>
<scope>test</scope>
</dependency>

Create a new test class for your controller. The sample controller looks something like this.

package com.ravi.springmvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("classpath:test-servlet-context.xml")

public class HomeControllerTest {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();
    //this.mockMvc = MockMvcBuilders.standaloneSetup(new HomeController()).build();
    }

    @Test
    public void getAccount() throws Exception {
    this.mockMvc.perform(get("/showlogs")).andExpect(status().isOk());
    }

}

There are two options to test 

1) With all your configurations, just like the above code. You can load your spring configuration file so that you can even see whether there are any issues in your configurations. 
2) As a standalone test without loading any configurations.  You can strip away all the configuration loading part and still test the application. Pretty impressive.

MockMvc is the core part of the test framework and as the name suggests mocks the MVC setup. When you call webAppContextSetup by passing WebApplicationContext you get the MockMVC instance. 

        this.mockMvc = MockMvcBuilders.webAppContextSetup(this.wac).build();

In that you can call perform method with the url to mimic the browser behavior of calling a particular URL path. 

     this.mockMvc.perform(get("/showlogs")).andExpect(status().isOk());

After the perform method you can test your expectations and basic one is whether the page returns ok by checking the http status code.

Standalone setup is even pretty simpler.

package com.ravi.springmvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Before;
import org.junit.Test;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

public class HomeControllerTest {

    private MockMvc mockMvc;

    @Before
    public void setup() {
     this.mockMvc = MockMvcBuilders.standaloneSetup(new HomeController()).build();
    }

    @Test
    public void getAccount() throws Exception {
    this.mockMvc.perform(get("/showlogs")).andExpect(status().isOk());
    }

}

1 comment:

 
Blogger Templates