转载:https://blog.csdn.net/maiyikai/article/details/78483423

本来要写springboot集成netty实现的,但是想起来单元测试没总结,那就趁此机会总结一下,不用文字了,直接用代码去实现: 
创建一个放方法的类MethodTest.java:(命名不严谨,能看懂就行了)

package com.mtk.netty.test;

import org.springframework.stereotype.Component;

@Component
public class MethodTest { //主方法
public String gg(boolean flag){
System.err.println("coming.........");
String d = g(flag);//子方法
h();
System.err.println("result data is "+d);
return d;
} public String g(boolean flag){
System.err.println("coming.........g");
if(flag){
throw new IllegalAccessError();//flag为true时抛异常
}
return "d";
} public void h(){
System.err.println("coming.........h");
}
}

使用上边的方法类,写单元测试: 
这里时spy方式:

package com.mtk.netty.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import com.myk.Application; /**
* 使用spy方式
* @author maiyikai 2017.11.08
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class SpyTest {
//gg(boolean)方法:控制台打印: coming.........
//g(boolean)方法:控制台打印: coming.........g
//h()方法:控制台打印: coming.........h
//gg(boolean)方法在g(boolean)和h()方法执行完之后:控制台打印: result data is * /**
* 使用代理:如下方式
* 或Mockito.spy(MethodTest.class)获得代理对象
*/
@SpyBean
private MethodTest spyTest; /**
* 不代理,直接运行程序
* 控制台打印:
* coming.........
* coming.........g
* coming.........h
* result data is d
* d
*/
@Test
public void test(){
System.err.println(spyTest.gg(false));
} /**
* 不使用代理:让g方法抛异常
* 控制台打印
* coming.........
* coming.........g
* 由于抛异常,程序中断
*/
@Test
public void test2(){
System.err.println(spyTest.gg(true));
} /**
* 使用代理对象,代理g方法,并让其返回“test”
* 第一种方式:使用thenReturn("test");
* 控制台打印:
* coming.........g
* coming.........
* coming.........h
* result data is test
* test
*
*/
@Test
public void test3(){
Mockito.when(spyTest.g(false)).thenReturn("test"); System.err.println(spyTest.gg(false));
}
/**
* 使用代理对象,代理g方法,并让其返回“test”
* 第二种方式:使用doReturn("test");
* 其中doReturn("test")后执行.when(spyTest).g(false)方法
* 控制台打印:
* coming.........
* coming.........h
* result data is test
* test
*
*/
@Test
public void test4(){
Mockito.doReturn("test").when(spyTest).g(false);
System.err.println(spyTest.gg(false));
}
/**
* test3-test4比较
* 正常执行顺序
* coming.........
* coming.........g
* coming.........h
* result data is d
* d
* 使用Mockito.when(spyTest.g(false)).thenReturn("test");方式
* g(boolean)方法会在调用主方法gg(boolean)前执行,不管执行的结果是啥,都会按照mock的数据“test”返回
*
* 使用Mockito.doReturn("test").when(spyTest).g(false);方式
* g(boolean)方法不会被执行,当时会将它的返回值赋值为:“test”。
* 在主方法gg(boolean)执行到g(boolean)时,不执行g(boolean)方法体,直接将值“test'”返回。后续方法照常执行
*/
//#######################################################################################
/**
* 使用代理对象,代理gg方法,并让其返回“test”
* 第一种方式:使用thenReturn("test");
* 控制台打印:
* coming.........g
*
*/
@Test
public void test5(){
Mockito.when(spyTest.g(true)).thenReturn("test");
System.err.println(spyTest.gg(true));
} /**
* 使用代理对象,代理gg方法,并让其返回“test”
* 第二种方式:使用doReturn("test");
* 其中doReturn("test")后执行.when(spyTest).g(true)方法--抛异常
* 控制台打印:
* coming.........
* coming.........h
* result data is test
* test
*
*/
@Test
public void test6(){
Mockito.doReturn("test").when(spyTest).g(true);
System.err.println(spyTest.gg(true));
} /**
* test5-test6比较
* 故意赋值让其方法抛异常。
* 正常执行顺序
* coming.........
* coming.........g
* coming.........h
* result data is d
* d
*
* 因为:Mockito.when(spyTest.g(true)).thenReturn("test");会执行方法体的内容,则g(boolean)方法体会被执行,会抛出异常中断程序运行
* 而Mockito.doReturn("test").when(spyTest).g(true);方法体不会被执行,则不会抛出异常,不会造成程序中断
*/ //##################################无返回值######################
/**
* 无返回值方法的代理
* 方法为h()
* 因此方法对代码不造成任何影响,所以可以不执行它
* 可以使用:doNothing
* 代码:Mockito.doNothing().when(spyTest).h();
*/
@Test
public void test7(){
Mockito.doNothing().when(spyTest).h(); System.err.println(spyTest.gg(false));
} /**
* 测试
* coming.........
* coming.........g
* result data is d
* d
*/
@Test
public void test10(){
Mockito.doReturn("23").when(spyTest).g(true);
Mockito.doNothing().when(spyTest).h(); System.err.println(spyTest.gg(false));
}
}

这里时mock的方式:

package com.mtk.netty.test;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration; import com.myk.Application; /**
* 使用mock方式
* @author maiyikai 2017.11.08
*
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = Application.class)
@WebAppConfiguration
public class MockTest { //gg(boolean)方法:控制台打印: coming.........
//g(boolean)方法:控制台打印: coming.........g
//h()方法:控制台打印: coming.........h
//gg(boolean)方法在g(boolean)和h()方法执行完之后:控制台打印: result data is * /**
* 使用代理:如下方式
* 或Mockito.mock(MethodTest.class)获得代理对象
*/
@MockBean
private MethodTest mockTest; /**
* 使用mock方式将会把对象给mock掉,则对象中的方法将不会被执行,但是使用到某方法时可以mock值,供调用方法使用
*/ /**
* 访问主方法体gg(boolean)
* 返回null
*/
@Test
public void test1(){
System.err.println(mockTest.gg(false));
} /**
* mock方法gg(boolean),使其在使用到gg(boolean)方法时,返回:"test"
*/
@Test
public void test2(){
Mockito.when(mockTest.gg(false)).thenReturn("test"); System.err.println(mockTest.gg(false));
}
/**
* mock方法gg(boolean),使其在使用到gg(boolean)方法时,返回:"test"
*/
@Test
public void test3(){
Mockito.doReturn("test").when(mockTest).gg(false); System.err.println(mockTest.gg(false));
}
/**
* mock方法gg(boolean),使其在使用到gg(boolean)方法时,返回:"test"
*/
@Test
public void test4(){
Mockito.when(mockTest.gg(false)).thenAnswer(new Answer<String>() {
//Answer<T> T为方法的返回值类型
@Override
public String answer(InvocationOnMock invocation) throws Throwable {
return "test";
}
}); System.err.println(mockTest.gg(false));
}
/**
* mock方法gg(boolean),使其在执行到此方法时抛出自定义异常
*/
@Test
public void test5(){
Mockito.when(mockTest.gg(false)).thenThrow( new IllegalArgumentException("单元测试异常")); System.err.println(mockTest.gg(false));
} //对与返回值类型为void的操作有
//1.直接mock对象,即此对象中所有的方法都不会被执行
//2.使用Mockito.doNothing().when(mockObject).method();标记执行该方法时不做任何操作
}

因时间有限,写了一些简单的操作; 
写单元测试的过程中,有想过“只需要mock方法类中的某个方法,其他的方法不受影响”;之前使用MockBean就出问题了,研究了一下使用SpyBean就可以使用了,可以看上边的例子就可以知道了。 
springboot的文档介绍也是可以看的:https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html “springboot 
我的项目上传地址:http://download.csdn.net/download/maiyikai/10110450

Springboot单元测试(MockBean||SpyBean)的更多相关文章

  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单元测试

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

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

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

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

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

随机推荐

  1. Revit API修改链接文件房间边界

    start [Transaction(TransactionMode.Manual)] [Regeneration(RegenerationOption.Manual)] );//设置房间边界     ...

  2. .NET基于Eleasticsearch搭建日志系统实战演练

    一.需求背景介绍 1.1.需求描述 大家都知道C/S架构模式的客户端应用程序(比如:WinForm桌面应用.WPF.移动App应用程序.控制台应用程序.Windows服务等等)的日志记录都存储在本地客 ...

  3. 大数据以及Hadoop相关概念介绍

    一.大数据的基本概念 1.1.什么是大数据 大数据指的就是要处理的数据是TB级别以上的数据.大数据是以TB级别起步的.在计算机当中,存放到硬盘上面的文件都会占用一定的存储空间,例如: 文件占用的存储空 ...

  4. Opencv2教程一:图像变换之阈值二值threshold

    网名:无名   QQ:16349023 email:mengwzy@qq.com 曾经非常少写教程,写的可能有点乱希望大对家有帮助 threshold 方法是通过遍历灰度图中点.将图像信息二值化,处理 ...

  5. delphi版本修改PE头源码

    //VC++6外衣 1 OEPCODEFIVE: THEAD = ($55, $8B, $EC, $6A, $FF, $68, $00, $00, $00, $00, $68, $00, $00, $ ...

  6. Nginx rewrite URL examples with and without redirect address

    原文地址: http://www.claudiokuenzler.com/blog/436/nginx-rewrite-url-examples-with-without-redirect-addre ...

  7. 【C++】STL常用容器总结之五:双端队列deque

    6.双端队列deque 所谓的deque是”double ended queue”的缩写,双端队列不论在尾部或头部插入元素,都十分迅速.而在中间插入元素则会比较费时,因为必须移动中间其他的元素.双端队 ...

  8. SharePoint 2013 workflows stop working (Failed on started.)

    前言 最近,使用工作流的时候碰到了一个问题,突然间所有工作流都无法启动,报错Failed on started. 同时,工作流内部报错,工作流被系统账号取消了. 查了很久,发现系统打了windows ...

  9. SharePoint JavaScript API in application pages

    前言 最近,在SharePoint 应用程序页中写JavaScript API,进行一些数据交互.其实,很简单的事情却遇到了问题,记录一下,希望能对遇到类似问题的人以帮助. 引用JavaScript ...

  10. 根据ImageView的大小来压缩Bitmap,避免OOM

    Bitmap是引起OOM的罪魁祸首之一,当我们从网络上下载图片的时候无法知道网络图片的准确大小,所以为了节约内存,一般会在服务器上缓存一个缩略图,提升下载速度.除此之外,我们还可以在本地显示图片前将图 ...