SpringBoot Test集成测试
来源:互联网 发布:查重率软件 编辑:程序博客网 时间:2024/06/12 05:58
如何测试SpringBoot的请求?使用spring-boot-starter-test
这个包即可完成测试,SpringBoot项目为什么需要测试本章不作过多说明,重点放在测试代码上。
使用说明
导包
gradle项目
compile group: 'com.fasterxml.jackson.jaxrs', name:'jackson-jaxrs-xml-provider',version:'2.5.0'compile group: 'org.springframework.boot', name: 'spring-boot-starter-test', version: '1.5.3.RELEASE'
maven项目
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-test --><dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>1.5.3.RELEASE</version> <scope>test</scope></dependency>
还需要依赖junit包,大家自行导入
关键核心测试例子
@RunWith(SpringRunner.class)@SpringBootTest(classes = Application.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)@EnableAutoConfigurationpublic class BBTestAA { @Autowired private TestRestTemplate testRestTemplate; //Application.class 为SpringBoot的启动入口类,每个SpringBoot项目大家都会配置}
GET请求测试
@Test public void get() throws Exception { Map<String,String> multiValueMap = new HashMap<>(); multiValueMap.put("username","lake");//传值,但要在url上配置相应的参数 ActResult result = testRestTemplate.getForObject("/test/get?username={username}",ActResult.class,multiValueMap); Assert.assertEquals(result.getCode(),0); }
POST请求测试
@Test public void post() throws Exception { MultiValueMap multiValueMap = new LinkedMultiValueMap(); multiValueMap.add("username","lake"); ActResult result = testRestTemplate.postForObject("/test/post",multiValueMap,ActResult.class); Assert.assertEquals(result.getCode(),0); }
文件上传测试
@Test public void upload() throws Exception { Resource resource = new FileSystemResource("/home/lake/github/wopi/build.gradle"); MultiValueMap multiValueMap = new LinkedMultiValueMap(); multiValueMap.add("username","lake"); multiValueMap.add("files",resource); ActResult result = testRestTemplate.postForObject("/test/upload",multiValueMap,ActResult.class); Assert.assertEquals(result.getCode(),0); }
文件下载测试
@Test public void download() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.set("token","xxxxxx"); HttpEntity formEntity = new HttpEntity(headers); String[] urlVariables = new String[]{"admin"}; ResponseEntity<byte[]> response = testRestTemplate.exchange("/test/download?username={1}",HttpMethod.GET,formEntity,byte[].class,urlVariables); if (response.getStatusCode() == HttpStatus.OK) { Files.write(response.getBody(),new File("/home/lake/github/file/test.gradle")); } }
请求头信息传输测试
@Test public void getHeader() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.set("token","xxxxxx"); HttpEntity formEntity = new HttpEntity(headers); String[] urlVariables = new String[]{"admin"}; ResponseEntity<ActResult> result = testRestTemplate.exchange("/test/getHeader?username={username}", HttpMethod.GET,formEntity,ActResult.class,urlVariables); Assert.assertEquals(result.getBody().getCode(),0); }
PUT
@Test public void putHeader() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.set("token","xxxxxx"); MultiValueMap multiValueMap = new LinkedMultiValueMap(); multiValueMap.add("username","lake"); HttpEntity formEntity = new HttpEntity(multiValueMap,headers); ResponseEntity<ActResult> result = testRestTemplate.exchange("/test/putHeader", HttpMethod.PUT,formEntity,ActResult.class); Assert.assertEquals(result.getBody().getCode(),0); }
DELETE
@Test public void delete() throws Exception { HttpHeaders headers = new HttpHeaders(); headers.set("token","xxxxx"); MultiValueMap multiValueMap = new LinkedMultiValueMap(); multiValueMap.add("username","lake"); HttpEntity formEntity = new HttpEntity(multiValueMap,headers); String[] urlVariables = new String[]{"admin"}; ResponseEntity<ActResult> result = testRestTemplate.exchange("/test/delete?username={username}", HttpMethod.DELETE,formEntity,ActResult.class,urlVariables); Assert.assertEquals(result.getBody().getCode(),0); }
阅读全文
2 0
- SpringBoot Test集成测试
- Spring Test 集成测试
- Spring test 集成测试 续
- spring集成测试(spring-test.jar)
- springboot test
- SpringBoot+MyBatis框架的项目单元测试+集成测试(上)
- 【Java.JUnit】Spring Test, JUnit, Mockito, Hamcrest 集成 Web 测试
- Spring Test, JUnit, Mockito, Hamcrest 集成 Web 测试
- Mybatis集成Spring MVC,使用Spring test进行测试
- springBoot junit Test
- SpringBoot+Junit Test
- Springboot中的test
- springboot测试
- springboot 集成hibernate 集成struts2
- springboot测试环境中为什么会将mongodb的数据自动存储到test表中
- springboot集成dubbo
- SpringBoot集成Swagger
- SpringBoot之集成SpringDataRedis
- DP--斜率优化--BZOJ1096--仓库建设
- Android 之Service利剑 bindservice,利用Messenger互相通信
- 找出数组中缺失的两个数(数组长度为98,数值范围1-100,数值无重复)
- Caffe学习笔记:cifar10_quick_train_test.prototxt配置文件分析
- win7安装和使用Django的出现的问题
- SpringBoot Test集成测试
- MD04读取的一些计算表
- 线程
- Hadoop入门全集(零)
- JavaWeb 修炼之路(六)请求和响应中的编码
- Android Device Monitor使用
- 合并、联合、融合和聚合之间的区别
- 嵌入式C语言编译器:GCC
- js自执行函数的几种不同写法的比较