内容简介:版权声明:本文为博主原屙文章,喜欢你就担走。 https://blog.csdn.net/leftfist/article/details/88876933
版权声明:本文为博主原屙文章,喜欢你就担走。 https://blog.csdn.net/leftfist/article/details/88876933
如前一篇文章( 《spring boot web api》 )所述,spring boot项目里提供web api非常方便。而作为开发人员,自己写的api,首先要自己先测试一轮,通过才能给其他人调用。API就是用来调用的,所以沟通、测试显得特别重要。
程序员测试,当然是单元测试了。下面是一个完整的单元测试代码。待测试的API是POST访问方式的api,有两个:一个提交参数格式为json,一个为键值对。
import api.entity.Author;
import api.entity.EnumSex;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
@RunWith(SpringRunner.class)
@SpringBootTest
public class MyTest {
@Autowired
private WebApplicationContext wac;
private MockMvc mockMvc;
@Before
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
}
@After
public void after() throws Exception {
mockMvc = null;
}
@Test
public void testJson() throws Exception {//测试提交参数为JSON格式
Author req = new Author();//Author是自定义的类
req.setName("chenqu");
req.setDesc("foolish");
req.setSex(EnumSex.Male);
ObjectMapper mapper = new ObjectMapper();
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
java.lang.String requestJson = ow.writeValueAsString(req);
mockMvc.perform(MockMvcRequestBuilders.post("/api/authors/json/1?t=108")
.contentType(MediaType.APPLICATION_JSON).content(requestJson))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn().getResponse().getContentAsString();
System.out.println("ok!!!");
}
@Test
public void testKv() throws Exception {//测试提交参数为键值对格式
Author req = new Author();
req.setName("chenqu");
req.setDesc("foolish");
req.setSex(EnumSex.Male);
ObjectMapper mapper = new ObjectMapper();
ObjectWriter ow = mapper.writer().withDefaultPrettyPrinter();
java.lang.String requestJson = ow.writeValueAsString(req);
mockMvc.perform(MockMvcRequestBuilders.post("/api/authors/kv/1?t=108")
.contentType(MediaType.APPLICATION_FORM_URLENCODED).content(requestJson))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andReturn().getResponse().getContentAsString();
System.out.println("ok!!!");
}
}
附API:
@RestController
@RequestMapping(value="/api")
public class ApiController {
//提交的参数为json
@RequestMapping(value = "/authors/json/{id}", method = RequestMethod.POST)
public String test(@PathVariable int id, @RequestParam("t") int type,@RequestBody Author auth){
return String.format("id:%1$d,type:%2$d,name:%3$s;params type:json",id,type,auth.getName());
}
//提交的参数为键值对
@RequestMapping(value = "/authors/kv/{id}", method = RequestMethod.POST)
public String test2(@PathVariable int id, @RequestParam("t") int type,Author auth){
return String.format("id:%1$d,type:%2$d,name:%3$s;params type:key-value",id,type,auth.getName());
}
}
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网
猜你喜欢:- Vue 应用单元测试的策略与实践 02 - 单元测试基础
- Vue 应用单元测试的策略与实践 04 - Vuex 单元测试
- Vue 应用单元测试的策略与实践 03 - Vue 组件单元测试
- Angular单元测试系列-Component、Directive、Pipe 以及Service单元测试
- 单元测试,只是测试吗?
- 单元测试和集成测试业务
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
Kafka权威指南
Neha Narkhede、Gwen Shapira、Todd Palino / 薛命灯 / 人民邮电出版社 / 2017-12-26 / 69.00元
每个应用程序都会产生数据,包括日志消息、度量指标、用户活动记录、响应消息等。如何移动数据,几乎变得与数据本身一样重要。如果你是架构师、开发者或者产品工程师,同时也是Apache Kafka新手,那么这本实践指南将会帮助你成为流式平台上处理实时数据的专家。 本书由出身于LinkedIn的Kafka核心作者和一线技术人员共同执笔,详细介绍了如何部署Kafka集群、开发可靠的基于事件驱动的微服务,......一起来看看 《Kafka权威指南》 这本书的介绍吧!
HTML 编码/解码
HTML 编码/解码
URL 编码/解码
URL 编码/解码