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应用 ...
随机推荐
- angular自定义管道
原文地址 https://www.jianshu.com/p/5140a91959ca 对自定义管道的认识 管道的定义中体现了几个关键点: 1.管道是一个带有“管道元数据(pipe metadata) ...
- 数据结构与算法实验题 7.1 M 商人的求救
问题描述: A 国正面临着一场残酷的战争,城市被支持不同领导的两股势力占据,作为一个商人,M先生并不太关心政治,但是他知道局势很严重,他希望你能救他出去.M 先生说:"为了安全起见,我们的路 ...
- 中小研发团队架构实践之RabbitMQ快速入门及应用
原文:中小研发团队架构实践之RabbitMQ快速入门及应用 使用过分布式中间件的人都知道,程序员使用起来并不复杂,常用的客户端API就那么几个,比我们日常编写程序时用到的API要少得多.但是分布式中间 ...
- [Node.js] Use nodejs-dashboard event loop delay with hrtime()
In this lesson, you will learn how to use the Formidable nodejs-dashboard event loop delay to identi ...
- 【C++竞赛 G】Lines
Time Limit: 3s Memory Limit: 64MB 问题描述 Ljr has several lines. The lines are covered on the X axis. L ...
- PatentTips - 在物联网中进行数据过滤的方法和装置
背景技术 [0001] 本发明涉及物联网,特别涉及在物联网进行数据过滤的方法和装置. [0002] 物联网是新一代信息技术的重要组成部分,特指物物相连的网络.具体地,物联网是指通过各种信息传感设备,如 ...
- WeakRefence
http://183615215-qq-com.iteye.com/blog/1867568
- 复制相关参数学习笔记--slave上的参数
server_id server_uuid relay_log io_thread 读取过来的本地日志. relaylog文件名前缀,可以是全路径. relay_log_index relay ...
- Erlang基础知识集锦
http://wenku.baidu.com/link?url=or-8mkUYUM0uVeqCYESGe93YIlh2IDLP7lFOwRlwr8Syf3PeHbwJC5DPCErs4NFrb1p4 ...
- kindeditor4跨域上传图片解决
项目中正在使用kindeditor, 版本号4.1.10 非常多公司的图片会走CDN,须要单独的一台图片上传服务如:(upload.268xue.com) kindeditor上传图片的简单内部流程: ...