官网参考地址

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 集成单元测试的更多相关文章

  1. Spring Boot 2.X 如何快速集成单元测试?

    本文将详细介绍下使用Spring Boot 2.X 集成单元测试,对API(Controller)测试的过程. 一.实现原理 使用MockMvc发起请求,然后执行API中相应的代码,在执行的过程中使m ...

  2. SpringBoot集成Mybatis并具有分页功能PageHelper

    SpringBoot集成Mybatis并具有分页功能PageHelper   环境:IDEA编译工具   第一步:生成测试的数据库表和数据   SET FOREIGN_KEY_CHECKS=0;   ...

  3. SpringBoot集成PageHelper时出现“在系统中发现了多个分页插件,请检查系统配置!”

    近日在项目中使用SpringBoot集成PageHelper后,跑单元测试时出现了"在系统中发现了多个分页插件,请检查系统配置!"这个问题. 如下图所示: org.mybatis. ...

  4. springboot集成mail实现邮件服务

    1,新建mailconfig类,对邮件服务固定配置进行实体封装: import java.util.Properties; import org.springframework.beans.facto ...

  5. springboot集成drools的方式一

    springboot集成drools的方式一(spring-drools.xml) 本文springboot采用1.5.1.RELEASE版本,drools采用的6.5.0.Final,一共会讲三种方 ...

  6. 图数据库Neo4j的基本使用及与SpringBoot集成

    Neo4j 官网地址:https://neo4j.com/ 下载地址:https://neo4j.com/download-center/#community 官方入门文档:https://neo4j ...

  7. 【springBoot】springBoot集成redis的key,value序列化的相关问题

    使用的是maven工程 springBoot集成redis默认使用的是注解,在官方文档中只需要2步; 1.在pom文件中引入即可 <dependency> <groupId>o ...

  8. SpringBoot集成security

    本文就SpringBoot集成Security的使用步骤做出解释说明.

  9. springboot集成Actuator

    Actuator监控端点,主要用来监控与管理. 原生端点主要分为三大类:应用配置类.度量指标类.操作控制类. 应用配置类:获取应用程序中加载的配置.环境变量.自动化配置报告等与SpringBoot应用 ...

随机推荐

  1. term- xshell VS securecrt

    Xshell使用技巧总结 https://www.cnblogs.com/zhangwuji/p/7155575.html SecureCRT与Xshell简单对比 http://ju.outofme ...

  2. Java Queue的使用

    Queue的成员函数        add        增加一个元索                     如果队列已满,则抛出一个IIIegaISlabEepeplian异常       rem ...

  3. Call to a member function assign() on a non-object;thinkphp中报错

    这个在自己写的类中 需要function __construct(){parent::__construct();}继承父类构造函数 当发生这个错误的时候,需要在构造函数中集成父类构造

  4. oracle汉字占用字节长度

    1.        今天调查一个oracle数据库问题的时候,发如今11g中一个汉字占2个字节,在10g中占3个字节.导致将11g数据库中的数据导入到10g的时候总是出错.開始的时候还以为是11g和1 ...

  5. Shuttle ESB(三)——架构模型介绍(2)

    上一篇文章中,介绍了Shuttle ESB架构模型中的三个重要部分. 今天,我们继续介绍剩余的三个内容:模式和消息路由. 四.模式 Request/Response(请求/响应模式) 对基于Reque ...

  6. amazeui中的js插件有哪些(详解功能)

    amazeui中的js插件有哪些(详解功能) 一.总结 一句话总结: 二.amazeui中的js插件有哪些 1.UI 增强 警告框Alert 按钮交互Button 折叠面板Collapse 下拉组件D ...

  7. 【codeforces 782A】Andryusha and Socks

    [题目链接]:http://codeforces.com/contest/782/problem/A [题意] 如果手套没有成一双,那么其中的一只就会被放在桌子上; 问你桌子上手套的只数最多的时候有几 ...

  8. TensorFlow 学习(九)—— 初始化函数(概率分布函数 api、常数生成函数)

    在 TensorFlow 中,一个变量的值在被使用之前,其初始化过程需要被明确地调用. 1. 随机数生成函数 tensorflow 下的概率分布函数,一般用于对变量进行初始化,这里的变量显然是指神经网 ...

  9. js Map 遍历

    1. 在知道的key的情况下遍历map自然就跟数组一样的访. 2.在不知道key的情况下遍历map(此情况更多吧)    2.1  for(var key in Map){  };  //第一种   ...

  10. Web自动化工具对比

    首先说一下我对Web自动化测试与CS自动化测试的认识.从宏观对比都是通过脚本自动化完成功能的验证,区别不大.Web测试更为显著的浏览器兼容性.安全,以及与Web技术相关的表单测试.链接测试等,其实都是 ...