package cn.wang.customer.test.inspect;
import cn.wang.customer.inspect.domain.enums.InspectEnum;
import cn.wang.customer.interfaces.filter.VersionCheckFilter;
import cn.wang.framework.core.enums.ResultType;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import top.rdfa.framework.biz.ro.RdfaResult;
import java.io.File;
import java.util.Scanner;
/**
* ClassName: InspectTaskControllerTest
* Description:
* Author: xiaodangjia
* Date: 2021/10/19 10:04
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
@RunWith(SpringJUnit4ClassRunner.class)
// ApplicationRunner:项目启动类名称
@SpringBootTest(classes = ApplicationRunner.class)
public class InspectTaskControllerTest {
private MockMvc mockMvc;
// 自定义过滤器 可不写
@Autowired
private VersionCheckFilter versionCheckFilter;
@Autowired private WebApplicationContext webApplicationContext;
@Before public void setUp() {
//此种方式可通过spring上下文来自动配置一个或多个controller 并添加过滤器
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext)
.addFilters(versionCheckFilter)
.build();
//此种方式,手工指定想要的controller
// mockMvc = MockMvcBuilders.standaloneSetup(inspectTaskController).build();
}
@Test public void queryContact() throws Exception {
/* JSONObject params = new JSONObject();
params.put("contactPhoneNumber", 123L);
params.put("entId", 885L);*/
ContactQueryParam contactQueryParam = new ContactQueryParam();
contactQueryParam.setContactPhoneNumber(123L);
contactQueryParam.setEntId(855L);
MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/inspect/task/contact/query")
.content(JSONObject.toJSONString(contactQueryParam)).contentType(MediaType.APPLICATION_JSON))
.andReturn();
// 设置返回结果编码格式,防止中文乱码
mvcResult.getResponse().setCharacterEncoding(CHARACTER_ENCODING_UTF_8);
String contentAsString = mvcResult.getResponse().getContentAsString();
// 获取返回结果,转换实体对象,进行断言
RdfaResult rdfaResult = JSON.parseObject(contentAsString, RdfaResult.class);
System.out.println(JSON.toJSON(rdfaResult));
Assert.assertTrue(rdfaResult.isSuccess());
Assert.assertEquals(rdfaResult.getCode(), ResultType.SUCCESS.code());
Assert.assertEquals(rdfaResult.getMessage(), ResultType.SUCCESS.message());
Assert.assertNotNull(rdfaResult.getData());
}
}
Test启动类加上VM Options // 断言 -ea