首先在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 —— 单元测试的更多相关文章

  1. Springboot单元测试Junit深度实践

    Springboot单元测试Junit深度实践 前言 单元测试的好处估计大家也都知道了,但是大家可以发现在国内IT公司中真正推行单测的很少很少,一些大厂大部分也只是在核心产品推广单测来保障质量,今天这 ...

  2. springmvc,springboot单元测试配置

    1. springmvc单元测试配置 <dependency> <groupId>junit</groupId> <artifactId>junit&l ...

  3. SpringBoot单元测试中的事务和Session

    1.Springboot中使用junit编写单元测试,并且测试结果不影响数据库. 2.

  4. SpringBoot单元测试

    一.Service层Junit单元测试 需要的jar包 <dependency> <groupId>org.springframework.boot</groupId&g ...

  5. springboot(十二):springboot单元测试、打包部署

    单元测试 1.在pom包中添加spring-boot-starter-test包引用 <dependency> <groupId>org.springframework.boo ...

  6. springboot系列三、springboot 单元测试、配置访问路径、多个配置文件和多环境配置,项目打包发布

    一.单元测试 生成的demo里面包含spring-boot-starter-test :测试模块,包括JUnit.Hamcrest.Mockito,没有的手动加上. <dependency> ...

  7. Springboot单元测试(MockBean||SpyBean)

    转载:https://blog.csdn.net/maiyikai/article/details/78483423 本来要写springboot集成netty实现的,但是想起来单元测试没总结,那就趁 ...

  8. 五、springboot单元测试

    1.为什么要写测试用例 1. 可以避免测试点的遗漏,为了更好的进行测试,可以提高测试效率 2. 可以自动测试,可以在项目打包前进行测试校验 3. 可以及时发现因为修改代码导致新的问题的出现,并及时解决 ...

  9. springBoot单元测试-基础单元测试

    1)在pom文件中加入junit支持 <!-- spring-boot-starter-test 单元测试 --> <dependency> <groupId>or ...

  10. 【使用篇二】SpringBoot单元测试(10)

    SpringCloud单元测试:https://www.cnblogs.com/myitnews/p/11796321.html 1. 创建项目Maven Project,修改pom.xml < ...

随机推荐

  1. TensorRT&Sample&Python[fc_plugin_caffe_mnist]

    本文是基于TensorRT 5.0.2基础上,关于其内部的fc_plugin_caffe_mnist例子的分析和介绍. 本例子相较于前面例子的不同在于,其还包含cpp代码,且此时依赖项还挺多.该例子展 ...

  2. 在 .NET Core 中结合 HttpClientFactory 使用 Polly(下篇)

    译者:王亮作者:Polly 团队原文:http://t.cn/EhZ90oq声明:我翻译技术文章不是逐句翻译的,而是根据我自己的理解来表述的(包括标题).其中可能会去除一些不影响理解但本人实在不知道如 ...

  3. Eruda 一个被人遗忘的移动端调试神器

    引言 ​ 日常工作中再牛逼的大佬都不敢说自己的代码是完全没有问题的,既然有问题,那就也就有调试,说到调试工具,大家可能对于fiddler.Charles.chrome devtools.Firebug ...

  4. iview inoput type=textarea 禁止拉伸

    设置 :maxRows.minRows相同即可 <Input v-model="formValidate.remark" type="textarea" ...

  5. vue2.0里的路由钩子

    路由钩子 在某些情况下,当路由跳转前或跳转后.进入.离开某一个路由前.后,需要做某些操作,就可以使用路由钩子来监听路由的变化 全局路由钩子: router.beforeEach((to, from, ...

  6. CodeForces 1151B Dima and a Bad XOR

    题目链接:http://codeforces.com/contest/1151/problem/B 题目大意: 给定一个n*m的矩阵,里面存放的是自然数,要求在每一行中选一个数,把他们异或起来后结果大 ...

  7. Linux(Ubuntu)使用日记------tenserflow安装(pip安装法)

    其实步骤是很简单的,只是一开始在网上找了一份错误的教程的原因,掉入了坑. 安装过程: 1. 检查pip版本 pip3 -V 要求使用最新版的pip 9.0.1,如果不是,按照下面的方法安装最新的pip ...

  8. 如何批量修改网页 更新网站 一键保存 windows查看和排序

    批量打开需要修改的网页,一键保存:一个网站会由很多网页组成,当需要大量更新的时候,如果一个个进行打开修改,效率会很低,内容修改不多,且容易修改的时候,可以用editplus这种小编辑软件批量打开,批量 ...

  9. vue+webpack+vue-cli获取URL地址参数

    在没有使用webpack+vue router开发中,想要获取RUL传的参数地址,直接通过一个函数就可以获得. 比如在  www.test.com/test.html?sign=test  地址中,想 ...

  10. 2.5 time 模块