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框架项目和精准测试工具进行结合和应用,通过精准测试的数据穿透.数据采集.测试用例与代码的双向追溯.数据分析等一系列精准测试的特有功能,达到对项目质量的 ...
随机推荐
- C++大小写转换和性能
p.MsoNormal,li.MsoNormal,div.MsoNormal { margin: 0cm; margin-bottom: .0001pt; text-align: justify; f ...
- RDS和ROS使用小结
微软的RDS和linux下的ROS,都已经使用了一段时间,RDS已经很久不更新了,前景必然不如ROS,但无奈用得顺手,还是偶尔怀旧一下. 使用RDS除了内置的文档需要仔细阅读,有些corobot.pr ...
- Leetcode_237_Delete Node in a Linked List
本文是在学习中的总结,欢迎转载但请注明出处:http://blog.csdn.net/pistolove/article/details/47334649 Write a function to de ...
- Myeclipse Db Browser使用
1.打开Myeclipse,选择菜单栏Window-->Show View-->Other,展开MyEclipse Database,选择DB Browser,打开数据库浏览视图 2. 空 ...
- "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no freetype in java.library.path
Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: no freetype in java ...
- 自动布局Autoresizing与Autolayout
一.关于iPhone屏幕的一些基本常识 1.ios屏幕适配的尺寸 iPhone的尺寸3.5inch.4.0inch.4.7inch.5.5inch iPad的尺寸7.9inch.9.7inch 2.点 ...
- VS2010断点调试技巧
设置断点:在如下图中的红色圆点处设置断点,红色圆点表示已经在这行设置断点.快捷键F9. 启动调试:按F5或者点击左边红框中的按钮.右边框是开始执行(不调试)Ctrl+F5. 调试工具栏:下面是工具 ...
- java基础语法(二)--单列模式
java基础语法(二)--单列模式 /** * 功能:单列模式 * @author Administrator * */ public class SingletonTest { public sta ...
- amaze UI 如何添加原生表单验证
这段时间做的一个项目,整个系统就一个页面,然后就是各种模态框,js里拼HTML代码,而且因为表单空留距离小,最后选定了amaze ui原生的表单验证 在amaze ui官网找到 表单验证. 但是ama ...
- Ng1从1.3开始的变更史
从今有个ng1 spa项目,项目可能会有ng1的版本升级问题,特简要摘录从1.3的主要版本变更,所以内容来自migration guide. 1.3的主要变更: 1.controller不能再以全局简 ...