站长资讯摘要:
使用Spring Boot 整合测试,对Controller 中某个方法进行测试或者对Service,Mapper等进行测试,不需要运行项目即可查看运行结果是否和期望值相同,Spring Boot整合了Junit测试,对测试目标进行单元测试又方便了一步。

TestController:

package com.edu.usts.controller;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
* Spring Boot整合测试
*/
@EnableAutoConfiguration
@Controller
public class TestController {

@RequestMapping("test")
@ResponseBody
public String test(){
return "test springboot";
}

public static void main(String[] args) {
SpringApplication.run(TestController.class,args);
}
}
这边返回测试值为 “test springboot”,后面我们使用TestCase.assertEquals(),对返回值和期望值之间进行比较。

正确案例:

package com.edu.usts.test;

import com.edu.usts.controller.TestController;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.security.RunAs;

/**
* Spring boot 整合测试
*/
@SpringBootTest(classes = TestController.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestSpringController {
@Autowired
private TestController testController;

@Test
public void test(){
TestCase.assertEquals(this.testController.test(),"test springboot");
}

}
运行截图:

绿色表示值对,则运行成功。我们修改测试值。

错误案例:

package com.edu.usts.test;

import com.edu.usts.controller.TestController;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;

import javax.annotation.security.RunAs;

/**
* Spring boot 整合测试
*/
@SpringBootTest(classes = TestController.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestSpringController {
@Autowired
private TestController testController;

@Test
public void test(){
TestCase.assertEquals(this.testController.test(),"test");
}

}
运行截图:

返回显示目标值和期望值不同,则提示错误。

项目截图:

忽略某些Controller,仅看Controller:TestController.java和测试:TestSpringController.java

pom依赖:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>study_sb</groupId>
<artifactId>study_sb</artifactId>
<version>1.0-SNAPSHOT</version>
<!-- 定义公共资源版本 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.6.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- Spring Boot Web 依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

</dependencies>
<build>
<plugins>
<!--控制JDK版本-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>

<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>

</plugins>
</build>
</project>

Spring Boot从入门到放弃-Spring Boot 整合测试的更多相关文章

  1. spring boot 学习入门篇【spring boot项目的搭建以及如何加载jsp界面】

    [ 前言]  Spring Boot 简介:Spring Boot是由Pivotal团队提供的全新框架,其设计目的是用来简化新Spring应用的初始搭建以及开发过程.该框架使用了特定的方式来进行配置, ...

  2. [Spring框架]Spring AOP基础入门总结二:Spring基于AspectJ的AOP的开发.

    前言: 在上一篇中: [Spring框架]Spring AOP基础入门总结一. 中 我们已经知道了一个Spring AOP程序是如何开发的, 在这里呢我们将基于AspectJ来进行AOP 的总结和学习 ...

  3. spring入门到放弃——spring事务管理

    Spring事务提供了两种管理的的方式:编程式事务和声明式事务 简单回顾下事务: 事务:逻辑上的一组操作,组成操作的各个单元,要么全部成功,要么全部失败. 事务特性: 原子性:一个事务包含的各个操作单 ...

  4. Spring Security框架入门

    1.Spring Security框架入门 1.1 Spring Security简介 Spring Security是一个能够为基于Spring的企业应用系统提供声明式的安全访问控制解决方案的安全框 ...

  5. 后端API入门到放弃指北

    后端API入门学习指北 了解一下一下概念. RESTful API标准] 所有的API都遵循[RESTful API标准]. 建议大家都简单了解一下HTTP协议和RESTful API相关资料. 阮一 ...

  6. Spring Boot 2从入门到放弃(持续更新)

    入门 Spring Boot 2项目的搭建和启动(入门篇1) Spring Boot 2项目的搭建和启动(入门篇2) spring boot 2项目自定义父pom Spring Boot 2开发工具s ...

  7. 优质分享 | Spring Boot 入门到放弃!!!

    持续原创输出,点击上方蓝字关注我 目录 前言 视频目录 如何获取? 总结 前言 最近不知不觉写Spring Boot专栏已经写了九篇文章了,从最底层的项目搭建到源码解析以及高级整合的部分,作者一直在精 ...

  8. Spring Boot 快速入门

    Spring Boot 快速入门 http://blog.csdn.net/xiaoyu411502/article/details/47864969 今天给大家介绍一下Spring Boot MVC ...

  9. Spring Boot快速入门(二):http请求

    原文地址:https://lierabbit.cn/articles/4 一.准备 postman:一个接口测试工具 创建一个新工程 选择web 不会的请看Spring Boot快速入门(一):Hel ...

随机推荐

  1. CSS 弹性盒子 flex的三个属性:grow、shrink、basis

    flex-grow 首先介绍flex-grow属性,flex-grow会在容器太大时(图片A.B的宽度和 < 父容器宽度)对元素作出调整. 如果图片A的flex-grow属性的值为 1,图片B的 ...

  2. Vue.js(18)之 axios简单封装

    基于vue-cli2.x封装axios src目录 axios.js import axios from 'axios' import { Indicator, Toast } from 'mint- ...

  3. DevOps专题|基础Agent部署系统

    随着京东云业务规模.管理机器规模的扩大,各类agent也在逐渐增多,如日志agent.监控agent.控制系统agent等.这对agent的部署.升级.状态维护提出了很高的要求,一旦某个全局agent ...

  4. Golang---sort包

    Sort 包介绍 Go 语言标准库 sort 包中实现了几种基本的排序算法:插入排序.快速排序和堆排序,但是在使用 sort 包进行排序时无需具体考虑使用哪种排序方式,因为该方法会根据传入的排序的数据 ...

  5. VUE中的MVVM模式

    1.传统MVP模式:业务逻辑相关的控制层 M:模型层,ajax请求 V:dom层,视图 P:控制器.js代码之类的 2.MVVM MVVM模式主要操作数据层,代码减少量是MVP的30%甚至70%

  6. JSP编码问题解决方法

    最近再看JSP相关知识,被中文乱码搞的很头大.找了好多方法终于找到了一个简单可行的方案. JSP中request和response操作默认编码为"ISO-8859-1",这是中文乱 ...

  7. 从1到n整数中1的个数

    [问题]求出1~13的整数中1出现的次数,并算出100~1300的整数中1出现的次数?为此他特别数了一下1~13中包含1的数字有1.10.11.12.13因此共出现6次,但是对于后面问题他就没辙了.A ...

  8. selenium破解人人登陆验证码

    from selenium import webdriverfrom PIL import Imagefrom chaojiying import Chaojiying_Clientimport ti ...

  9. Block实现代理/通知效果

    例子1:A控制器->跳转—>B控制器 , 假设想从B控制器回传数组给A控制器 实现:B控制器.h文件定义一个block参数,.m文件执行block,A控制器设置block内容 B.h文件/ ...

  10. java后台开发细节记录

    1.  ResultMap是程序员控制SQL查询结果和实体类的映射关系,而不是sql语句中字段的重命名,所以在sql语句中还是要按照原来字段的格式进行书写.