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

台州做网站免费制作网页平台

台州做网站,免费制作网页平台,织梦网站默认密码,如何做产品网站网页写完代码后,测试是必不可少的步骤,现在来介绍一下基于SpringBoot的测试方法。 基于SpringBoot框架写完相应功能的Controller之后,然后就可以测试功能是否正常,本博客列举MockMvc和RestTemplate两种方式来测试。 准备代码 实体类…

写完代码后,测试是必不可少的步骤,现在来介绍一下基于SpringBoot的测试方法。

基于SpringBoot框架写完相应功能的Controller之后,然后就可以测试功能是否正常,本博客列举MockMvcRestTemplate两种方式来测试。

准备代码

实体类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",method=RequestMethod.POST)public Person addUser(Person person){person.setId(UUID.randomUUID().toString().substring(0, 6));return person;}/*** 使用JSON方式传递数据* @param person 保存对象* @return*/@RequestMapping(value="/addJson",method=RequestMethod.POST)public Person addUserByJson(@RequestBody Person person){person.setId(UUID.randomUUID().toString().substring(0, 6));return person;}@RequestMapping(value="/get/{id}",method=RequestMethod.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 person=restTemplate.getForObject(baseUrl+"/get/001", Person.class);System.out.println(person);}@Testpublic void addPerson(){//当直接传递参数需要用mapMultiValueMap<String, Object> paramMap = new LinkedMultiValueMap<String, Object>();paramMap.add("name", "Aster");Person person=restTemplate.postForObject(baseUrl+"/add", paramMap, Person.class);System.out.println(person);}@Testpublic void addPersonByJson(){try{Person p = new Person("Banana");Person person=restTemplate.postForObject(baseUrl+"/addJson", p, Person.class);System.out.println(person);}catch (Exception e) {e.printStackTrace();}}
}

相比而言,RestTemplate比MockMvc更加简单,更加清晰。

http://www.hkea.cn/news/447069/

相关文章:

  • wordpress免费中文企业主题seo权重优化软件
  • 周口网站建设哪家好济南专业seo推广公司
  • 济南网站忧化怎么把抖音关键词做上去
  • 网站建设与维护的题目网站点击软件排名
  • 网站收录服务企业网络的组网方案
  • nba排名灰色词seo排名
  • 如何建自己的个人网站深圳市seo上词多少钱
  • 迎访问中国建设银行网站_永久免费的电销外呼系统
  • 类似AG网站建设网络营销的十大特点
  • 河北盘古做的网站用的什么服务器品牌策划与推广
  • 做网站开发的是不是程序员品牌营销与推广
  • 安卓android软件seo搜索引擎优化方式
  • 网站设计培训课程引流推广平台
  • 做淘宝美工需要知道的网站app软件推广平台
  • 做自己个人网站搜索竞价
  • 兰州网站优化哪家好手机系统流畅神器
  • 广东深圳住房和城乡建设部网站文章优化软件
  • java制作动态网站开发怎么可以让百度快速收录视频
  • 做网站管理好吗阳泉seo
  • 网站排名优化建设seo人人网
  • html5可以做动态网站惠州seo计费
  • 商城网站带宽控制河南网站建设哪家公司好
  • 贵阳网络公司网站建设网络推广公司深圳
  • 企业网站建设公司电话西安seo分析报告怎么写
  • 岳阳市政府网网站seo优化报告
  • 门头沟网站建设外贸谷歌推广
  • 铜陵市住房和城乡建设委员会网站中国最新疫情最新消息
  • 动态网站建设 教程接广告推广的平台
  • 人力资源和社会保障部是干什么的seo最新快速排名
  • 网站标题关键优化网络营销代运营外包公司