JUnit Test Spring MVC Web Services

Spring MVC REST Controller

package com.example.controller.api;

import java.math.BigInteger;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.model.CustomerActivity;
import com.example.service.CustomerActivityRepository;
import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiError;
import com.wordnik.swagger.annotations.ApiErrors;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

@Controller
@RequestMapping(value = "/api/v1/customeractivity")
@Api(value = "Customer operations", listingClass = "CustomerController", basePath = "/api/v1/customeractivity", description = "All operations for customers activity")
public class CustomerActivityController {
	
	@Autowired
	@Qualifier("mongoRepository")
	private CustomerActivityRepository customerService;
	
	 @ApiOperation(value = "Find customer by Id", notes = "Get customer by specifying Id", httpMethod = "GET", responseClass = "Customer", multiValueResponse = true)
	 @ApiErrors(value = { @ApiError(code = 400, reason = "Invalid ID supplied"), @ApiError(code = 404, reason = "Customer not found") })
	 @RequestMapping(value = "/find/{customerId}", method = RequestMethod.GET, produces = "application/json")
	 public @ResponseBody List<CustomerActivity> findCustomerById(@ApiParam(internalDescription = "java.lang.string", name = "customerId", required = true, value = "string") @PathVariable String customerId) {
		 	List<CustomerActivity> customer = customerService.findCustomerActivityByCustomerId(customerId);
	        return customer;
	 }
}

Spring MVC REST Junit Test

package com.example.controller.api;

import static org.junit.Assert.*;

import java.util.Arrays;
import java.util.Date;

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 com.example.model.CustomerActivity;
import com.example.service.CustomerActivityRepositoryImpl;

import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes={CustomerActivityController.class, CustomerActivityRepositoryImpl.class})
public class CustomerActivityControllerTest {
	
	private MockMvc mockMvc;
	
	@Autowired CustomerActivityController customerActivityController;
	
	@Before
	public void setup() {
		this.mockMvc =  MockMvcBuilders.standaloneSetup(customerActivityController).build();
	}
	
	@Test
	public void testCustomerActivityFindCustomerById() throws Exception {
		CustomerActivity customerActivity = new CustomerActivity("123", "SMS Received", new Date(), "SMS", "Workflow", "http://workflow.com");
		CustomerActivityController mock = org.mockito.Mockito.mock(CustomerActivityController.class);
		when(mock.findCustomerById("123")).thenReturn(Arrays.asList(customerActivity));
		
		this.mockMvc.perform(get("/api/v1/customeractivity/find/123"))
		.andExpect(status().isOk())
		.andExpect(content().contentType("application/json"))
		.andExpect(jsonPath("$[0].customerId").value("123"));
		verifyNoMoreInteractions(mock);
	}
}

Leave a Comment