经过入门篇,可以编写出一个简单的测试用例。

这篇讲的是BDDMockito的使用。

BDDMockito用于测试时进行打桩处理;通过它可以指定某个类的某个方法在什么情况下返回什么样的值。

在单元测试时,如果遇到复杂的业务场景,使用多个类时,就需要用到BDDMockito。

需要打桩的对象,只能是@Mock注解的方式

package xx.xx.test;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; @RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@WebAppConfiguration
public class ApplicationTests { @Before
public void init(){
System.out.println("******测试开始");
} @After
public void end(){
System.out.println("******测试结束");
} @BeforeClass
public static void initClass(){
System.out.println("******测试开始初始化");
} @AfterClass
public static void endClass(){
System.out.println("******测试结束初始化");
}
}
package xx.xx.test;

import org.mockito.BDDMockito;
import org.mockito.Mock;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.mock.mockito.MockBean; import xx.xx.test.InfoService;
import xx.xx.test.UserService; public class Test extends ApplicationTests{ @Mock
private UserService userService;
@Autowired
private InfoService infoService; @org.junit.Test
public void add(){
int i = userService.add("王五");
System.out.println("Test.add.i:" + i);
int j = infoService.update("张三");
System.out.println("Test.add.j:" + j);
} @Override
public void init(){
BDDMockito.given(userService.add("王五")).willReturn(9);
BDDMockito.given(userService.add("")).willReturn(0);
BDDMockito.given(userService.add(null)).willThrow(NullPointerException.class);
System.out.println("******重写测试开始");
}
}
package xx.xx.test;

public interface InfoService {

    Integer update(String name);

}
package xx.xx.test.impl;

import org.springframework.stereotype.Service;

import xx.xx.test.InfoService;

@Service("infoService")
public class InfoServiceImpl implements InfoService{ public Integer update(String name){
return 1;
} }
package xx.xx.test;

public interface UserService {

    Integer add(String name);

}

打印结果:

******测试开始

******重写测试开始
Test.add.i:9
Test.add.j:1
******测试结束
******测试结束初始化

BDDMockito有哪些使用场景呢?

1、可以指定打桩对象的返回值。

  1.1、使用given:

BDDMockito.given(userService.add("王五")).willReturn(9);
BDDMockito.given(userService.add("")).willReturn(0);
BDDMockito.given(userService.add(null)).willThrow(NullPointerException.class);

    上面代码的意思是:

      当传入的参数是"王五"的时候,返回值是9。

      当传入的参数是""时,返回的值是0。

      当传入的参数是null时,抛出NullPointerException异常。

SpringBoot中使用JNnit4(一)之Mockito的使用的更多相关文章

  1. SpringBoot中使用JNnit4(入门篇)

    一.添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId> ...

  2. 【快学springboot】在springboot中写单元测试[Happyjava]

    前言 很多公司都有写单元测试的硬性要求,在提交代码的时候,如果单测通不过或者说单元测试各种覆盖率不达标,会被拒绝合并代码.写单元测试,也是保证代码质量的一种方式. junit单元测试 相信绝大多数的J ...

  3. 【快学springboot】在springboot中写单元测试

    前言 很多公司都有写单元测试的硬性要求,在提交代码的时候,如果单测通不过或者说单元测试各种覆盖率不达标,会被拒绝合并代码.写单元测试,也是保证代码质量的一种方式. junit单元测试 相信绝大多数的J ...

  4. SpringBoot中yaml配置对象

    转载请在页首注明作者与出处 一:前言 YAML可以代替传统的xx.properties文件,但是它支持声明map,数组,list,字符串,boolean值,数值,NULL,日期,基本满足开发过程中的所 ...

  5. 如何在SpringBoot中使用JSP ?但强烈不推荐,果断改Themeleaf吧

    做WEB项目,一定都用过JSP这个大牌.Spring MVC里面也可以很方便的将JSP与一个View关联起来,使用还是非常方便的.当你从一个传统的Spring MVC项目转入一个Spring Boot ...

  6. springboot中swaggerUI的使用

    demo地址:demo-swagger-springboot springboot中swaggerUI的使用 1.pom文件中添加swagger依赖 2.从github项目中下载swaggerUI 然 ...

  7. spring-boot+mybatis开发实战:如何在spring-boot中使用myabtis持久层框架

    前言: 本项目基于maven构建,使用mybatis-spring-boot作为spring-boot项目的持久层框架 spring-boot中使用mybatis持久层框架与原spring项目使用方式 ...

  8. 由浅入深学习springboot中使用redis

    很多时候,我们会在springboot中配置redis,但是就那么几个配置就配好了,没办法知道为什么,这里就详细的讲解一下 这里假设已经成功创建了一个springboot项目. redis连接工厂类 ...

  9. Springboot中使用AOP统一处理Web请求日志

    title: Springboot中使用AOP统一处理Web请求日志 date: 2017-04-26 16:30:48 tags: ['Spring Boot','AOP'] categories: ...

随机推荐

  1. 在keil中添加stc系列单片机型号(模型)方法

    1.下载安装stc-isp烧录软件: 官网:http://www.gxwmcu.com/ 2.打开使用stc-isp软件,并导入stc官方器件库: 注意:一定要找到包含有C51和UV4的文件夹 3.显 ...

  2. 如何给Windows2016新建IIS并建立网站

    首先在菜单按钮旁右击-----点击Control Panel(控制面板) 点击Turn Windows features on or off 开始设置环境!!!!! 认真勾选参数!!! 然后点击安装! ...

  3. 使用PowerShell实时查看日志文件的变化

    开发过程中,会有好多的日志输出到日志文件中了,每次看日志都需要打开,log文件,觉得麻烦 找了个省事的方法 使用PowerShell 使用命令:Get-Content D:\www\webapp1\L ...

  4. jdbc连接数据库,中文出现乱码的问题

    一.使用jdbc连接数据库,插入数据库时,数据里的数据显示乱码,为 " ??? " 两种解决方案: 1.修改服务端的mysql配置文件,编辑my.cnf文件,在[mysqld]下添 ...

  5. 二维前缀和模板题:P2004 领地选择

    思路:就是使用二维前缀和的模板: 先放模板: #include<iostream> using namespace std; #define ll long long ; ll a[max ...

  6. .NET Core微服务之基于IdentityServer建立授权与验证服务(续)

    Tip: 此篇已加入.NET Core微服务基础系列文章索引 上一篇我们基于IdentityServer4建立了一个AuthorizationServer,并且继承了QuickStartUI,能够成功 ...

  7. 分布式事务解决方案FESCAR

    项目地址:FESCAR 以下是官网的文档.简介2019年,Fescar 是 阿里巴巴 开源的 分布式事务中间件,以 高效 并且对业务 0 侵入 的方式,解决 微服务 场景下面临的分布式事务问题. 1. ...

  8. 为什么使用 Redis 及其产品定位

    一:传统 MySQL+ Memcached 架构遇到的问题 实际 MySQL 是适合进行海量数据存储的,通过 Memcached 将热点数据加载到 cache,加速访问,很多公司都曾经使用过这样的架构 ...

  9. 树莓派播放视频的播放器omxplayer

    omxplyer为树莓派量身定做的一款GPU硬件加速的播放器,很好的解决了树莓派cpu计算力不足的缺点.(播放时cpu一定都不烫手) 1.安装方法: CTRL + ALT + T 调出终端命令行输入 ...

  10. Python从零搭建Conf_Web配置管理平台

    环境 CentOS 6/7 x64 Python:2 .7.6 Etcd: 3.2.18 Confd:0 .16.0 Nginx: 1.12.1 效果演示 一,拓扑图: 二.涉及软件 ETD: .分布 ...