欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Mockito单元测试之SpringMvc controller

程序员文章站 2022-04-29 16:44:31
...

创建测试类

package cn.******.jl.contract.api.controller;

import static org.junit.Assert.assertEquals;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import cn.******.jl.contract.model.entity.CreditStatus;
import cn.******.jl.contract.service.ContractService;
import cn.******.ms.common.entity.ResponseBean;
import cn.******.ms.common.entity.StatusCode;

@RunWith(MockitoJUnitRunner.class)
public class ContractControllerTest {
	private MockMvc mockMvc;

	@Mock
	ContractService contractService;

	@InjectMocks
	ContractController contractController;

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

	@Test
	public void getContractStatus_THEN_RETURN_SUCCESSFUL_NO_DATA() throws Exception {
		List<CreditStatus> statusList = new ArrayList<CreditStatus>();
		ResponseBean<List<CreditStatus>> responseBean = ResponseBean.success(statusList);
		when(contractService.getContractStatus(anyString())).thenReturn(responseBean);

		this.mockMvc.perform(get("/api/v1/contractStatus/10001")).andDo(print()).andExpect(status().isOk())
				.andExpect(content().contentType("application/json;charset=UTF-8"));
		verify(contractService, times(1)).getContractStatus("10001");
		assertEquals(StatusCode.SUCCESSFUL_NO_DATA.getCode(), responseBean.getCode());
	}

	@Test
	public void getContractStatus_THEN_RETURN_SUCCESSFUL() throws Exception {
		List<CreditStatus> statusList = new ArrayList<CreditStatus>();
		CreditStatus status1 = new CreditStatus();
		status1.setModifyTime(new Date());
		status1.setStatus("a");

		CreditStatus status2 = new CreditStatus();
		status2.setModifyTime(new Date());
		status2.setStatus("r");

		statusList.add(status1);
		statusList.add(status2);

		ResponseBean<List<CreditStatus>> responseBean = ResponseBean.success(statusList);
		when(contractService.getContractStatus(anyString())).thenReturn(responseBean);

		this.mockMvc.perform(get("/api/v1/contractStatus/10001")).andDo(print())
				.andExpect(status().isOk()).andExpect(content().contentType("application/json;charset=UTF-8"));
		verify(contractService, times(1)).getContractStatus("10001");
		assertEquals(StatusCode.SUCCESSFUL.getCode(), responseBean.getCode());
	}

	@Test
	public void getContractStatus_THEN_THROW_EXCEPTION_AND_RETURN_INTERNAL_ERROR() throws Exception {
		ResponseBean<List<CreditStatus>> responseBean = new ResponseBean<List<CreditStatus>>(
				StatusCode.INTERNAL_ERROR.getCode(), StatusCode.INTERNAL_ERROR.getMsg());
		when(contractService.getContractStatus(anyString())).thenReturn(responseBean);

		this.mockMvc.perform(get("/api/v1/contractStatus/10001")).andDo(print()).andExpect(status().isOk())
				.andExpect(content().contentType("application/json;charset=UTF-8"));
		verify(contractService, times(1)).getContractStatus("10001");
		assertEquals(StatusCode.INTERNAL_ERROR.getCode(), responseBean.getCode());
	}

	@Test
	public void getContractStatus_with_json_THEN_THROW_EXCEPTION_AND_RETURN_INTERNAL_ERROR() throws Exception {
		ResponseBean<List<CreditStatus>> responseBean = new ResponseBean<List<CreditStatus>>(
				StatusCode.INTERNAL_ERROR.getCode(), StatusCode.INTERNAL_ERROR.getMsg());
		when(contractService.getContractStatus(anyString())).thenReturn(responseBean);

		this.mockMvc
				.perform(get("/v1/api/dd/monitor/ddrealtimemonitor").accept(MediaType.APPLICATION_JSON)
						.param("query_date", "2019-03-22").param("business_type", "20").param("file_type", "1")
						.param("gc_type", "CFC").param("id_category", "1"))
				.andDo(print()).andExpect(status().isOk())
				.andExpect(content().contentType("application/json;charset=UTF-8"));
		verify(contractService, times(1)).getContractStatus("10001");
		assertEquals(StatusCode.INTERNAL_ERROR.getCode(), responseBean.getCode());
	}
}

 

另外还可以创建测试套件

package cn.******.jl.contract.api;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import cn.******.jl.contract.api.controller.ContractControllerTest;

@RunWith(Suite.class)
@SuiteClasses({ContractControllerTest.class})
public class TestSuits {
}