当前位置: 首页 > news >正文

php网站 php有什么用网站差异

php网站 php有什么用,网站差异,小说网站做编辑,php网站开发软件编程写完代码后#xff0c;测试是必不可少的步骤#xff0c;现在来介绍一下基于SpringBoot的测试方法。 基于SpringBoot框架写完相应功能的Controller之后#xff0c;然后就可以测试功能是否正常#xff0c;本博客列举MockMvc和RestTemplate两种方式来测试。 准备代码 实体类…写完代码后测试是必不可少的步骤现在来介绍一下基于SpringBoot的测试方法。 基于SpringBoot框架写完相应功能的Controller之后然后就可以测试功能是否正常本博客列举MockMvc和RestTemplate两种方式来测试。 准备代码 实体类Person public class Person {private String id;private String name;public Person() {}public Person(String name) {this.name name;}public String getId() {return id;}public void setId(String id) {this.id id;}public String getName() {return name;}public void setName(String name) {this.name name;}Overridepublic String toString() {return Person [id id , name name ];}}控制器PersonController import javax.validation.Valid;import org.springframework.validation.BindingResult; import org.springframework.validation.ObjectError; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController;import com.test.springboottest.bean.Person;RestController RequestMapping(/person) public class PersonController {/*** 使用对象方式传递数据* param person 保存对象* return*/RequestMapping(value/add,methodRequestMethod.POST)public Person addUser(Person person){person.setId(UUID.randomUUID().toString().substring(0, 6));return person;}/*** 使用JSON方式传递数据* param person 保存对象* return*/RequestMapping(value/addJson,methodRequestMethod.POST)public Person addUserByJson(RequestBody Person person){person.setId(UUID.randomUUID().toString().substring(0, 6));return person;}RequestMapping(value/get/{id},methodRequestMethod.GET)public Person getUser(PathVariable String id){Person person new Person(Mepper);person.setId(id);return person;} }上述代码即为简化版的数据的增查的功能。 MockMvc方式 import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;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.setup.MockMvcBuilders; import org.springframework.web.context.WebApplicationContext;import com.fasterxml.jackson.databind.ObjectMapper; import com.test.springboottest.bean.Person;SpringBootTest//系统会自动加载Spring Boot容器 RunWith(SpringRunner.class) public class ControllerTest {//模拟http请求private MockMvc mockMvc;//用于将对象转换为json字符串private ObjectMapper mapper new ObjectMapper();Autowiredprivate WebApplicationContext context;Beforepublic void setUp(){mockMvc MockMvcBuilders.webAppContextSetup(context).build();}//测试数据获取Testpublic void getPerson(){try {mockMvc.perform(MockMvcRequestBuilders.get(/person/get/2018001) //请求的url,请求的方法是get.accept(MediaType.APPLICATION_JSON_UTF8)).andDo(print());//打印出请求和相应的内容.andReturn().getResponse().getContentAsString(); //将相应的数据转换为字符串} catch (Exception e) {e.printStackTrace();}}//测试数据的添加Testpublic void addPerson(){try {mockMvc.perform(MockMvcRequestBuilders.post(/person/add).param(name, Apple) //添加参数.accept(MediaType.APPLICATION_JSON_UTF8)).andDo(print());} catch (Exception e) {e.printStackTrace();}}//测试JSON字符串的保存Testpublic void addPersonByJson(){try {Person person new Person(Banana);String requestBody mapper.writeValueAsString(person);mockMvc.perform(MockMvcRequestBuilders.post(/person/addJson).contentType(MediaType.APPLICATION_JSON_UTF8) //数据的格式.content(requestBody) .accept(MediaType.APPLICATION_JSON_UTF8)).andDo(print());} catch (Exception e) {e.printStackTrace();}} }mockMvc.perform执行一个RequestBuilder请求MockMvcRequestBuilders.get:构造一个get请求。另外提供了其他的请求的方法如post、put、delete等param添加request的参数root的参数。假如使用需要发送json数据格式的时将不能使用这种方式可见后面被ResponseBody注解参数的解决方法contentType指定传递的数据类型accept 指定接受的数据类型andDo添加ResultHandler结果处理器比如调试时打印结果到控制台对返回的数据进行的判断andReturn最后返回相应的MvcResult然后进行自定义验证/进行下一步的异步处理对返回的数据进行的判断 注意点 当使用JSON传递数据的时候需要使用.contentType(MediaType.APPLICATION_JSON_UTF8).content(requestBody)的方式 不然会发生org.springframework.http.converter.HttpMessageNotReadableException异常因为相应方法只接受JSON数据格式。RestTemplate方式 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.junit4.SpringRunner; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate;import com.test.springboottest.bean.Person;SpringBootTest(webEnvironment SpringBootTest.WebEnvironment.RANDOM_PORT) RunWith(SpringRunner.class) public class ControllerRestTest {Value(http://localhost:${local.server.port}/person)private String baseUrl;private RestTemplate restTemplate new RestTemplate();Testpublic void getPerson(){Person personrestTemplate.getForObject(baseUrl/get/001, Person.class);System.out.println(person);}Testpublic void addPerson(){//当直接传递参数需要用mapMultiValueMapString, Object paramMap new LinkedMultiValueMapString, Object();paramMap.add(name, Aster);Person personrestTemplate.postForObject(baseUrl/add, paramMap, Person.class);System.out.println(person);}Testpublic void addPersonByJson(){try{Person p new Person(Banana);Person personrestTemplate.postForObject(baseUrl/addJson, p, Person.class);System.out.println(person);}catch (Exception e) {e.printStackTrace();}} }相比而言RestTemplate比MockMvc更加简单更加清晰。
http://www.w-s-a.com/news/375262/

相关文章:

  • 怎么用wordpress建立自己的网站加强校园网站建设
  • 用什么做网站后台的织梦网站怎么上传
  • 怎么获取网站数据做统计百度快照推广有效果吗
  • 淘宝领卷网站什么做制造网站开发
  • 如何做com的网站网站建设投标书模板
  • 郑州网络营销网站优化网站技术方案怎么写
  • 济南市住房和城乡建设局网站wordpress mnews主题
  • ios开发网站app网站建设企业有哪些方面
  • 网站主页 优帮云深圳代做网站后台
  • app 与网站网站建设要做什么
  • 厦门国外网站建设公司郑州核酸点推vip服务
  • 免费网线seo外链怎么做
  • 宽带技术网网站wordpress widget hook
  • 山西省住房和城乡建设厅网站报名wordpress添加标签插件
  • 网站怎么自己做外贸网站案例
  • 做网站的优势公司网站怎么做站外链接
  • 海城网站制作建设精准营销的营销方式
  • 北京短视频拍摄公司重庆网站seo推广公司
  • 广州免费推广网站建设4399网页游戏大全
  • 网站的构架与组成建站公司兴田德润
  • php网站部署步骤邯郸哪有做网站的
  • 做设计什么设计比较好的网站南充市住房和城乡建设局考试网站
  • 郑州做系统集成的公司网站龙岩
  • 厦门SEO_厦门网站建设网络营销课程视频
  • vs 2015 网站开发开网店在线咨询
  • 前端如何优化网站性能大学学校类网站设计
  • 中国铁路建设投资公司网站熊学军中国it外包公司排名前50
  • 房产网站的建设广州推广排名
  • 湟源县网站建设wordpress删除未分类
  • 营销型网站开发推广厦门百度seo公司