import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.ResultActions;
import java.util.ArrayList;
import java.util.List;
import static org.mockito.BDDMockito.given;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Created by ji_pengyun on 17-7-11.
*/
@RunWith(SpringJUnit4ClassRunner.class)
@WebMvcTest(MaintCompletionEvaluationController.class)
//新创建一个启动类。去掉mybatis的mapperScan注解。
@ContextConfiguration(classes = {com.chinamobile.cmss.test_support.ConfigurationForTest.class})
public class MaintCompleteEvaluationControllerTest {
@MockBean
private MaintCompletionEvaluationService maintCompleteEvaluationService;
@MockBean
private MaintPointResponseService maintPointResponseService;
@MockBean
private TaskService taskService;
@MockBean
private SubTaskService subTaskService;
@MockBean
private MaintCompleteService maintCompleteService;
@MockBean
private BusinessDataCompleteService businessDataCompleteService;
@Autowired
private MockMvc mvc;
//get 请求
@Test
public void testGetMaintCompletionEvaluationSum() throws Exception {
//创建测试数据
MaintCompletionEvaluation maintCompletionEvaluation = new MaintCompletionEvaluation();
List<MaintPointResponse> maintPointResponses = new ArrayList<>();
MaintPointResponse maintPointResponse = new MaintPointResponse();
maintPointResponses.add(maintPointResponse);
maintCompletionEvaluation.setIndicatorsJson("
{\"provinceId\": 200,\"subTaskId\":172,\"saveType\":2,\"gsmOgicBaseStationYearAvg\": 10,\"status\":2}");
maintCompletionEvaluation.setContactsJson("{\"gsmOgicBaseStationYearAvgNote\": 6}");
maintCompletionEvaluation.setSubTaskId(172);
maintCompletionEvaluation.setProvinceId(200);
maintCompletionEvaluation.setContactsJson("
{\"gsmOgicBaseStationYearAvgEVAL_END\": 6,\"gsmOgicBaseStationYearAvgCONTACT_END\": 6,\"gsmOgicBaseStationYearAvgPHONE_END\": 6}");
maintCompletionEvaluation.setIndicatorsJson("{\"provinceId\": 200,\"subTaskId\":172,\"saveType\":2,\"gsmOgicBaseStationYearAvg\": 10,\"status\":2}");
List<MaintCompletionEvaluation> maintCompletionEvaluations = new ArrayList<>();
maintCompletionEvaluations.add(maintCompletionEvaluation);
Long deadLine = System.currentTimeMillis() + 100000;
Integer status = 1;
SubTask subTask = new SubTask();
subTask.setId(1);
subTask.setSummarySourceJson("
{\"maintCompleteCurrentYear\": 2017,\"maintCompleteCurrentStageNumber\":2,\"businessDataCompleteCurrentYear\":2017,\"businessDataCompleteCurrentStageNumber\":2,\"maintCompleteLastYear\":2016,\"maintCompleteLastStageNumber\": 2,\"businessDataCompleteLastYear\":2016,\"businessDataCompleteLastStageNumber\": 2,\"benchMarkingsId\": 12}");
Integer result = 1;
String summarySourceJson = "
{\"maintCompleteCurrentYear\": 2017,\"maintCompleteCurrentStageNumber\":2,\"businessDataCompleteCurrentYear\":2017,\"businessDataCompleteCurrentStageNumber\":2,\"maintCompleteLastYear\":2016,\"maintCompleteLastStageNumber\": 2,\"businessDataCompleteLastYear\":2016,\"businessDataCompleteLastStageNumber\": 2,\"benchMarkingsId\": 12}";
String instruction = "instruction";
String templateId = "templateId";
//service
given(this.taskService.getDeadLineBySubTaskId(Matchers.any(Integer.class)))
.willReturn(deadLine);
given(this.taskService.getTaskStatusBySubTaskId(Matchers.any(Integer.class), Matchers.any(Integer.class), Matchers.any(Integer.class)))
.willReturn(status);
given(this.taskService.getSubTaskBySubTaskIdAndType(Matchers.any(Integer.class), Matchers.any(Integer.class)))
.willReturn(subTask);
given(this.maintCompleteEvaluationService.addOrUpdateMaintCompletionEvaluationSammary(Matchers.any(MaintCompletionEvaluation.class)))
.willReturn(result);
given(this.maintCompleteEvaluationService.getMaintCompletionEvaluationSum(Matchers.any(Integer.class), Matchers.any(Integer.class)))
.willReturn(maintCompletionEvaluations);
given(this.subTaskService.getBenchMarkingIdBySubTaskId(Matchers.any(Integer.class)))
.willReturn(summarySourceJson);
given(this.taskService.getInstructionBySubTaskId(Matchers.any(Integer.class)))
.willReturn(instruction);
given(this.taskService.getTemplateIdBySubTaskId(Matchers.any(Integer.class)))
.willReturn(templateId);
given(this.maintPointResponseService.getMaintPointResponseSummaries(Matchers.any(Integer.class), Matchers.any(Integer.class)))
.willReturn(maintPointResponses);
//构建请求
final ResultActions perform = this.mvc.perform(get("/maint_completion_evaluation?subTaskId=172&provinceId=200").header("User-Name", "aa"));
perform.andExpect(handler().handlerType(MaintCompletionEvaluationController.class));
perform.andExpect(status().isOk());
perform.andExpect(content().contentType("application/json;charset=UTF-8"));
perform.andExpect(jsonPath("$.meta").exists());
perform.andExpect(jsonPath("$.data.details[0].provinceId").value(200));
perform.andExpect(jsonPath("$.data.details[0].subTaskId").value(172));
//另一个请求为了if else,完成覆盖
final ResultActions perform1 = this.mvc.perform(get("/maint_completion_evaluation?subTaskId=172&provinceId=0").header("User-Name", "ji"));
perform1.andExpect(handler().handlerType(MaintCompletionEvaluationController.class));
perform1.andExpect(status().isOk());
perform1.andExpect(content().contentType("application/json;charset=UTF-8"));
perform1.andExpect(jsonPath("$.meta").exists());
perform1.andExpect(jsonPath("$.data.details[0].provinceId").value(200));
perform1.andExpect(jsonPath("$.data.details[0].subTaskId").value(172));
}
//post请求
@Test
public void testAddBusinessDataComplete() throws Exception {
BusinessDataComplete businessDataComplete = new BusinessDataComplete();
businessDataComplete.setCreateTime(121321123L);
businessDataComplete.setProvinceId(200);
Integer result = 0;
Long deadLine = System.currentTimeMillis() + 100000;
given(this.maintCompleteEvaluationService.addOrUpdateMaintCompletionEvaluation(Matchers.any(MaintCompletionEvaluation.class)))
.willReturn(result);
given(this.taskService.getDeadLineBySubTaskId(Matchers.any(Integer.class)))
.willReturn(deadLine);
final ResultActions perform = this.mvc.perform(post("/maint_completion_evaluation")
.contentType(MediaType.APPLICATION_JSON)
.header("User-Name", "ji")
.content("
{\"provinceId\": 551,\"subTaskId\": 20,\"saveType\":2,\"wireTotalOptiPerContact\": \"jj\",\"wireTotalOptiPerPhone\": \"18712121212\",\"wireTotalMaintEval\":\"花花\",\"wireTotalMaintContact\": \"hh\",\"wireTotalMaintPhone\": \"18731112222\",\"wireGsmEval\": \"gssf\"}"));
final ResultActions perform1 = this.mvc.perform(post("/maint_completion_evaluation")
.contentType(MediaType.APPLICATION_JSON)
.header("User-Name", "ji")
.content("{\"provinceId\": 551,\"subTaskId\": 20,\"saveType\":2,\"wireTotalMaintContact\": \"hh\"}"));
final ResultActions perform2 = this.mvc.perform(post("/maint_completion_evaluation")
.contentType(MediaType.APPLICATION_JSON)
.header("User-Name", "ji")
.content("{\"provinceId\": 551,\"subTaskId\": 20,\"saveType\":2,\"wireTotalOptiPerPhone\": \"18712121212\"}"));
}
}
controller单元测试代码
最新推荐文章于 2023-11-18 19:06:55 发布
