Spring Boot 1.4测试的简单理解
首先maven要引入spring-boot-starter-test
这个包。
先看一段代码
@RunWith(SpringRunner.class) @SpringBootTest(webEnvironment=WebEnvironment.RANDOM_PORT) public class MyTest { @Autowired private TestRestTemplate restTemplate; @Test public void test() { this.restTemplate.getForEntity( "/{username}/vehicle", String.class, "Phil"); } }
1、@RunWith(SpringRunner.class) 告诉JUnit运行使用Spring的测试支持。SpringRunner是SpringJUnit4ClassRunner的新名字,这个名字只是让名字看起来简单些。
2、@SpringBootTest的意思是“带有Spring Boot支持的引导程序”(例如,加载应用程序、属性,为我们提供Spring Boot的所有精华部分)。
3、webEnvironment属性允许为测试配置特定的“网络环境”。你可以利用一个MOCK小服务程序环境开始你的测试,或者使用一个运行在RANDOM_PORT 或者 DEFINED_PORT上的真正的HTTP服务器。
4、如果我们想要加载一个特定的配置,我们可以用@SpringBootTest class属性。在这个实例中,我们省略classes就意味着测试要首次尝试从任意一个inner-classes中加载@ configuration,如果这个尝试失败了,它会在你主要的@SpringBootApplicationclass中进行搜索。
------------------------------------------------------------------------------------------------------------------------------
其中IndexController是你写的controller
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class IndexTest { private MockMvc mvc; @Autowired
private IndexController indexController; @Before
public void setup() {
this.mvc = MockMvcBuilders.standaloneSetup(indexController).build();
} @Test
public void t() throws Exception {
assertNotNull(mvc); mvc.perform(MockMvcRequestBuilders.get("/h").
accept(MediaType.parseMediaType("application/json;charset=UTF-8")))
.andExpect(MockMvcResultMatchers.status().isOk()); }
}
这个是第二种测试方法:
package com.hexun.bdc.auth.controller; import com.hexun.bdc.auth.client.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers; import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertNotNull; /**
* Created by iceblue on 2017/3/26.webEnvironment=RANDOM_PORT
表示启动服务时端口随机(避免端口冲突)
*/ @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = Application.class)
@AutoConfigureMockMvc
public class IndexControllerTest { @Autowired
protected MockMvc mvc; @LocalServerPort // 注入端口
private Integer port; @Autowired
private TestRestTemplate restTemplate; private static final Logger logger = LoggerFactory.getLogger(IndexControllerTest.class); @Test
public void t() throws Exception {
assertNotNull(mvc); logger.info("# port:" + port.toString()); assertThat(this.restTemplate.getForObject("http://localhost:" + port + "/h",
String.class)).contains("hello");
// mvc.perform(MockMvcRequestBuilders.get("/h"))
// .andExpect(MockMvcResultMatchers.status().isOk());
} }
是第三种测试方法,直接利用TestRestTemplate类
package com.hexun.bdc.auth.controller; import com.hexun.bdc.auth.client.Application;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertNotNull; @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,classes = Application.class)
public class IndexTest2 { @Autowired
private TestRestTemplate restTemplate;
private static final Logger logger = LoggerFactory.getLogger(IndexTest2.class); @Test
public void t() throws Exception {
assertNotNull(restTemplate);
String body = this.restTemplate.getForObject("/h", String.class);
System.out.println("body = " + body);
logger.info("body = " + body); } }
Spring Boot 1.4测试的简单理解的更多相关文章
- spring boot项目如何测试,如何部署
有很多网友会时不时的问我,spring boot项目如何测试,如何部署,在生产中有什么好的部署方案吗?这篇文章就来介绍一下spring boot 如何开发.调试.打包到最后的投产上线. 开发阶段 单元 ...
- Spring Boot中的测试
文章目录 简介 添加maven依赖 Repository测试 Service测试 测试Controller @SpringBootTest的集成测试 Spring Boot中的测试 简介 本篇文章我们 ...
- Spring Boot应用的测试——Mockito
Spring Boot应用的测试——Mockito Spring Boot可以和大部分流行的测试框架协同工作:通过Spring JUnit创建单元测试:生成测试数据初始化数据库用于测试:Spring ...
- 如何使用Dubbo 2.7.0和Spring boot实现FAT测试(Feature Acceptance Test)
在一个调用链非常长的功能中,如果想修改其中的一个特性,并进行测试,而又不影响该环境的其他用户使用现有功能.特性,例如: 1. A.B.C.D之间通过Dubbo实现远程调用 2. 这些模块可能有一个或者 ...
- Spring Boot 1.4测试的改进
对Pivotal团队来说,工作上的好事情是他们拥有一个被叫做Pivotal Labs的灵活发展部门,拥有Labs团队的Lean 和 XP程序设计方法学的强大支持,例如结对编程和测试驱动开发.他们对于测 ...
- Spring Boot 解决方案 - JUnit 测试
简单的 JUnit 项目 回顾一下创建并运行简单的 JUnit 测试项目,先添加 JUnit 依赖然后编写类似如下模板的测试类,使用 IDE 的话直接用插件运行就行, 使用 Maven 的话运行命令 ...
- Spring Boot【快速入门】简单案例
Spring Boot[快速入门] Spring Boot 概述 Build Anything with Spring Boot:Spring Boot is the starting point ...
- Spring Boot集成Springfox Swagger3和简单应用
摘要:Springfox Swagger可以动态生成 API 接口供前后端进行交互和在线调试接口,Spring Boot 框架是目前非常流行的微服务框架,所以,在Spring Boot 项目中集成Sp ...
- 【星云测试】开发者测试-采用精准测试工具对Spring Boot应用进行测试
简介:本文主要介绍把现今主流的springboot框架项目和精准测试工具进行结合和应用,通过精准测试的数据穿透.数据采集.测试用例与代码的双向追溯.数据分析等一系列精准测试的特有功能,达到对项目质量的 ...
随机推荐
- Invalid Subledger (XLA) Packages In Release 12.1.3
In this Document Goal Solution 1.- Information about These Packages 2.- Solution Reference ...
- 如何使用VS2013本地C++单元测试框架
在VS2013中,可以使用VS自带的C++单元测试框架. 在使用该框架前,需要先安装Unit Test Generator(可以通过菜单“工具->扩展和更新”搜索安装). 下边,就阐述一下利用该 ...
- Gradle 1.12用户指南翻译——第三十七章. OSGi 插件
本文由CSDN博客万一博主翻译,其他章节的翻译请参见: http://blog.csdn.net/column/details/gradle-translation.html 翻译项目请关注Githu ...
- PS 滤镜——Skewing
%%%% Skewing clc; clear all; close all; addpath('E:\PhotoShop Algortihm\Image Processing\PS Algorith ...
- java的io库用到的装饰模式是如何体现的?
概论 java的io包下大概有85个类,真复杂.其实不然这些类又可以分为以下四个部分. 输入流 输出流 字节流 InputStream ...
- Linux内核中断和异常分析(中)
在linux内核中,每一个能够发出中断请求的硬件设备控制器都有一条名为IRQ的输出线.所有现在存在的IRQ线都与一个名为可编程中断控制器的硬件电路的输入引脚相连,上次讲到单片机的时候,我就讲到了单片机 ...
- Spring 学习笔记 Bean的作用域
在配置文件中定义Bean时,用户不但可以配置Bean的属性值以及相互之间的依赖关系,还可以定义Bean的作用域.作用域将对Bean的生命周期和创建方式产生影响.在低版本的Spring中,仅有两个作用域 ...
- LeetCode(52)-Remove Linked List Elements
题目: Remove all elements from a linked list of integers that have value val. Example Given: 1 --> ...
- Set对象常用操作方法和遍历
Set<String> set = new HashSet<String>(); /** * set的常用操作方法有: * add()向集合添加元素 clear()清空集合元素 ...
- vue学习:props,scope,slot,ref,is,slot,sync等知识点
1.ref :为子组件指定一个索引 ID,给元素或者组件注册引用信息.refs是一个对象,包含所有的ref组件. <div id="parent"> <user- ...