4. SpringBoot —— 单元测试
首先在pom文件中引入spring-boot-starter-test
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
测试方式1:
这也是官方推荐的方式,缺点是在真正的多项目开发时,有的项目没有启动类,为了这种方式的测试,还需在 src/test 下构建一个测试启动类
@RunWith(SpringRunner.class)
@SpringBootTest(classes=App.class)
public class RedisUtilTest { @Autowired
private RedisUtil redisUtil ; @Test
public void testAddOrUpdate() {
redisUtil.addOrUpdate("test","ddd");
} }
测试方式2 (人个总结出的方法):
使用 SpringApplicationBuilder 类构建 ApplicationContext实例,但是这种方式需要明确知道相应的@Configuration配置类
1. 定义项目的Configuration类,相当于指定了配置文件
@Configuration
@ConditionalOnMissingBean(RedisUtil.class)
public class EcshopCommonUtilsConfig { @Bean
public RedisUtil redisUtil() {
return new RedisUtil() ;
} }
2.通过SpringApplicationBuilder构建context容器
public class RedisUtilTest {
@Autowired
private RedisUtil redisUtil ;
@Before
public void init() {
Class<?>[] sources = {XxxxConfig.class,RedisAutoConfiguration.class} ;
String[] properties = {"spring.redis.host=127.0.0.1","spring.redis.port=6379","spring.redis.database=0","spring.redis.password=xxx"} ;
SpringApplicationBuilder builder = new SpringApplicationBuilder().properties(properties)
.web(WebApplicationType.NONE) ;
builder.sources(sources) ;
ConfigurableApplicationContext context = builder.run() ;
this.redisUtil = context.getBean(RedisUtil.class) ;
}
@Test
public void testAddOrUpdate() {
redisUtil.addOrUpdate("test","ddd");
}
}
4. SpringBoot —— 单元测试的更多相关文章
- Springboot单元测试Junit深度实践
Springboot单元测试Junit深度实践 前言 单元测试的好处估计大家也都知道了,但是大家可以发现在国内IT公司中真正推行单测的很少很少,一些大厂大部分也只是在核心产品推广单测来保障质量,今天这 ...
- springmvc,springboot单元测试配置
1. springmvc单元测试配置 <dependency> <groupId>junit</groupId> <artifactId>junit&l ...
- SpringBoot单元测试中的事务和Session
1.Springboot中使用junit编写单元测试,并且测试结果不影响数据库. 2.
- SpringBoot单元测试
一.Service层Junit单元测试 需要的jar包 <dependency> <groupId>org.springframework.boot</groupId&g ...
- springboot(十二):springboot单元测试、打包部署
单元测试 1.在pom包中添加spring-boot-starter-test包引用 <dependency> <groupId>org.springframework.boo ...
- springboot系列三、springboot 单元测试、配置访问路径、多个配置文件和多环境配置,项目打包发布
一.单元测试 生成的demo里面包含spring-boot-starter-test :测试模块,包括JUnit.Hamcrest.Mockito,没有的手动加上. <dependency> ...
- Springboot单元测试(MockBean||SpyBean)
转载:https://blog.csdn.net/maiyikai/article/details/78483423 本来要写springboot集成netty实现的,但是想起来单元测试没总结,那就趁 ...
- 五、springboot单元测试
1.为什么要写测试用例 1. 可以避免测试点的遗漏,为了更好的进行测试,可以提高测试效率 2. 可以自动测试,可以在项目打包前进行测试校验 3. 可以及时发现因为修改代码导致新的问题的出现,并及时解决 ...
- springBoot单元测试-基础单元测试
1)在pom文件中加入junit支持 <!-- spring-boot-starter-test 单元测试 --> <dependency> <groupId>or ...
- 【使用篇二】SpringBoot单元测试(10)
SpringCloud单元测试:https://www.cnblogs.com/myitnews/p/11796321.html 1. 创建项目Maven Project,修改pom.xml < ...
随机推荐
- webpack中插件 prerender-spa-plugin 来进行SEO优化(二十四)
vue.react对于开发单页应用来说带来了很好的用户的体验,但是同样有缺点,比如首页加载慢,白屏或SEO等问题的产生.为什么会出现这种情况呢?我们之前开发单页应用是这样开发的,比如首页 index. ...
- future builder
import 'package:flutter/material.dart';import 'dart:convert';import 'package:http/http.dart' as http ...
- shell 脚本中执行sql
#!/bin/bashsql='show databases' result=`mysql -uroot -pengine2user -h127.0.0.1 -e "${sql}" ...
- 路由信息对象Route之属性query和params的区别
query的使用 第一步:在<router-link/>标签中配置如下 <router-link :to="{name:'beijing',query:{id:1,user ...
- 【Swift 3.0】iOS 国际化切换语言
有的 App 可能有切换语言的选项,结合系统自动切换最简单的办法: fileprivate var localizedBundle: Bundle = { return Bundle(path: Bu ...
- windows服务器nginx+php启动开源ecshop
1,下载php,nginx,ECShop源码 2,解压php到指定目录(如:C:\php-7.2.6) 2.1,找到指定目录下文件php.ini-development复制重命名为php.ini 2. ...
- 2019微信公开课 同行With Us 听课笔记及演讲全文
[2019WeChat 微信公开课] 产品理念: 微信启动页 一个小人站在地球前面,每个人都有自己的理解和解读 所谓异类,表示与别人与众不同,即优秀的代名词. 微信的与众不同体现在尊重用户对产品的感受 ...
- svg(可缩放矢量图形)
入门推荐: http://www.ruanyifeng.com/blog/2018/08/svg.html (教程) http://www.runoob.com/svg/svg-reference.h ...
- POJ 3974 Palindrome (算竞进阶习题)
hash + 二分答案 数据范围肯定不能暴力,所以考虑哈希. 把前缀和后缀都哈希过之后,扫描一边字符串,对每个字符串二分枚举回文串长度,注意要分奇数和偶数 #include <iostream& ...
- 如何打印consul的错误信息
在配置文件中添加 management: endpoints: web: exposure: include: "*" endpoint: shutdown: enabled: t ...