springboot 集成单元测试
1. 添加依赖
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
添加完依赖以后如果POM文件中报错,类型
Failure to transfer org.hamcrest:hamcrest-core:jar:1.3 .... was chached in the local repository
这是说在本地有缓存,把本地repository里对应的包删除掉,然后右键项目->maven->update project,报哪个包的错就删掉那个包。
造成这个错误的原因是对应的包下存在有 .lastupdated文件,删掉这些文件就可以。
stackoverflow上有类似解决办法。
2.简单JAVA测试
在方法上加@Test注解,run as Junit即可
package UtilitiesTest; import org.junit.Assert;
import org.junit.Test; public class SimpleJavaTest { @Test
public void EquelTest(){
Integer i = 128;
Integer j = 128;
Assert.assertEquals(true, i != j);
}
}
结果

3. 测试service
写测试类:
package ServiceTest; import java.util.ArrayList; import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner; import com.pkyou.Sample.Main;
import com.pkyou.Sample.Entyties.IndoorCheckItemEntity;
import com.pkyou.Sample.ServiceImp.ControllerService;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Main.class)
@AutoConfigureMockMvc
public class ControllerServiceTest {
@Autowired
private ControllerService service;
private ArrayList<IndoorCheckItemEntity> entities; @Test
public void ResultTest(){
entities = service.GetIndoorCheckItemEntities();
Assert.assertNotNull(entities);
Assert.assertEquals(3, entities.size());
}
}
以上注解意义,测试方法参考官网。这种测试方法每次都会启动tomcat
测试结果:

4. 测试controller
测试类
package ControllerTest; import java.net.URL;
import java.util.ArrayList; import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import com.pkyou.Sample.Main;
import com.pkyou.Sample.Entyties.IndoorCheckItemEntity; @RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,classes = Main.class)
public class HelloControllerTest { @LocalServerPort
private int port;
private URL base;
@Autowired
private TestRestTemplate template; @Before
public void SetUp() throws Exception {
this.base = new URL("http://192.168.7.11:" + port + "/controller/GetIndoorCheckItemEntities");
} @Test
public void GetIndoorInfo() throws Exception{
ResponseEntity<Object> entities =
template.getForEntity(base.toString(),Object.class);
ArrayList<IndoorCheckItemEntity> body = (ArrayList<IndoorCheckItemEntity>)entities.getBody();
Assert.assertNotNull(body);
Assert.assertEquals(body.size(), 3);
}
}
springboot 集成单元测试的更多相关文章
- Spring Boot 2.X 如何快速集成单元测试?
本文将详细介绍下使用Spring Boot 2.X 集成单元测试,对API(Controller)测试的过程. 一.实现原理 使用MockMvc发起请求,然后执行API中相应的代码,在执行的过程中使m ...
- SpringBoot集成Mybatis并具有分页功能PageHelper
SpringBoot集成Mybatis并具有分页功能PageHelper 环境:IDEA编译工具 第一步:生成测试的数据库表和数据 SET FOREIGN_KEY_CHECKS=0; ...
- SpringBoot集成PageHelper时出现“在系统中发现了多个分页插件,请检查系统配置!”
近日在项目中使用SpringBoot集成PageHelper后,跑单元测试时出现了"在系统中发现了多个分页插件,请检查系统配置!"这个问题. 如下图所示: org.mybatis. ...
- springboot集成mail实现邮件服务
1,新建mailconfig类,对邮件服务固定配置进行实体封装: import java.util.Properties; import org.springframework.beans.facto ...
- springboot集成drools的方式一
springboot集成drools的方式一(spring-drools.xml) 本文springboot采用1.5.1.RELEASE版本,drools采用的6.5.0.Final,一共会讲三种方 ...
- 图数据库Neo4j的基本使用及与SpringBoot集成
Neo4j 官网地址:https://neo4j.com/ 下载地址:https://neo4j.com/download-center/#community 官方入门文档:https://neo4j ...
- 【springBoot】springBoot集成redis的key,value序列化的相关问题
使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...
- SpringBoot集成security
本文就SpringBoot集成Security的使用步骤做出解释说明.
- springboot集成Actuator
Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...
随机推荐
- Access Violations 访问冲突(AVs)是Windows编程时发生的最麻烦的错误?
Access Violations<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office&qu ...
- [Angular Directive] 2. Add Inputs to Angular 2 Directives
The @Input decorator allows you to pass values into your @Directive so that you can change the value ...
- Android自定义组件系列【4】——自定义ViewGroup实现双侧滑动
在上一篇文章<Android自定义组件系列[3]--自定义ViewGroup实现侧滑>中实现了仿Facebook和人人网的侧滑效果,这一篇我们将接着上一篇来实现双面滑动的效果. 1.布局示 ...
- [CSS] Change the auto-placement behaviour of grid items with grid-auto-flow
We can change the automatic behaviour of what order our grid items appear. We can even re-order the ...
- 【转】priority_queue的用法
http://www.cnblogs.com/flyoung2008/articles/2136485.html priority_queue调用 STL里面的 make_heap(), pop_he ...
- iOS中js与objective-c的简单交互
1.首先是objective-c调用js中的代码,可以用UIWebview中的一个方法 stringByEvaluatingJavaScriptFromString:后面接的是js中的方法名.这个函数 ...
- js声明json数据,打印json数据,遍历json数据,转换json数据为数组
1.js声明json数据: 2.打印json数据: 3.遍历json数据: 4.转换json数据为数组; //声明JSON var json = {}; json.a = 1; //第一种赋值方式(仿 ...
- Delphi新手跟我学写CALL,附完整原程序
在开始进入正题前先罗嗦几句: 1.本人也刚学Delphi不久,也刚通过<诛仙>游戏的绝大部分CALL不久.所以在以下所说所列举的例子并不算是名门正中的写法,如有不当,请各位原谅. 2.本人 ...
- 几种tab切换尝试 原生js
<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...
- 【BZOJ 1032】 [JSOI2007]祖码Zuma
[题目链接]:http://www.lydsy.com/JudgeOnline/problem.php?id=1032 [题意] [题解] /* 设f[i][j]表示从第i个珠子开始的j个珠子被消除; ...