标签:自动 etc content cat orm form 4.0 case tar
---恢复内容开始---
@SpringBootTest注解是SpringBoot自1.4.0版本开始引入的一个用于测试的注解
1.添加maven依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
2.Controller右键->new一个test case,选择自己要生成案例的方法,点击完成。
3.使用MockMvc进行单元测试,MockMvc是唯一支持Controller测试的。
测试类如下:
private MockMvc mockMvc;
@Autowired
private WebApplicationContext webApplicationContext;
@Before
public void init() {
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
}
// 机密秘钥
final String salt = "11";
final String key = "miyao";
public static final Charset CHARSET_UTF8 = Charset.forName("UTF-8");
@Test
public void testRegister() {
// fail("Not yet implemented");
UusRegisterReq req = new UusRegisterReq();
req.setParterKey("KE");
req.setParterSource(2);
req.setParterUesrId("1");
req.setPhone("13122221111");
String str = FastJsonUtils.obj2json(req);
RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/uus/register").content(str)
.contentType(MediaType.APPLICATION_JSON);
try {
String res = mockMvc.perform(requestBuilder).andExpect(MockMvcResultMatchers.status().isOk()).andReturn()
.getResponse().getContentAsString();
log.info("注册测试结果:{}", res);
} catch (Exception e) {
e.printStackTrace();
}
}
标签:自动 etc content cat orm form 4.0 case tar
原文地址:https://www.cnblogs.com/lucky-girl/p/9477483.html