Mockito

Mockito是一个模拟测试框架,常用于Java单元测试。本文介绍了Mockito的核心元素,如Mock、Stub和Spy,以及如何进行参数匹配、stubbing void方法、定制Answer、检验行为、校验调用顺序、使用真实对象的Spy、注解简化mock对象创建等实战技巧。文章还强调了Mockito在测试中的注意事项和局限性。
摘要由CSDN通过智能技术生成

Mockito

Dan North, the originator of Behavior-Driven Development wrote this back in 2008:
“We decided during the main conference that we should use JUnit 4 and Mockito because we think they are the future of TDD and mocking in Java”


说明
本文章中的示例使用的开发环境为JDK1.8!
Mockito的版本信息如下

<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>

文章中的示例部分来自Mockito Dzone Reference Card

Warning:文章中的示例只是为了展示Mockito的语法和特性,在实际的测试编码中,我们可能不会使用示例的用法(测试逻辑)!
官方原文:

Warning: Note that the examples in this Refcardwere created to demonstrate behaviors of Mockito in a specific context. Of course, when writing the test for your codebase, there is no need to ensure that mocks are stubbed correctly.

鄙人在Github上修改了部分示例,并且写了一些其他的示例(实际测试代码中用法)!传送门



简介

Mockito是一个模拟测试框架。在一个被测试的对象(功能)A中,它通常需要与其他的对象(功能)B进行一些交互,我们把A称作被测试对象(tested object),把B称作协作者(collaborators)。那么在测试环境中,这些协作者都需要被创建,以便被测试对象可以使用它们。为了使测试代码简化以及满足上下文执行环境,我们通常使用测试替身(test double)来代替这些协作者,测试替身看上去和原始的协作者一样,但是却不依赖其他对象,而且可以执行预期行为,记录他们的交互行为(interactions,可以理解成方法调用)


核心元素

TDD : Test-Driven Development

BDD : Behavior-Driven Development

Mock : 模拟对象,可以理解成Mockito框架帮我们自动生成的数据。通过mock产生的对象有以下的能力:1.可以通过编程产生预期行为;2.在对象的生命周期内可以校验它的交互行为(方法调用);

原文参考:Mock - an object with the ability to a) have a programmed expected behavior, and b) verify the interactions occurring in its lifetime (this object is usually created with the help of mocking framework)

Stub : 存根?桩?可以理解成通过硬编码的方式预期定义行为(方法),将会产生特定的结果,屏蔽原本行为(方法)的实现。

/**举例,伪代码描述*/

//define a method
public string sayABC() {
   
    return "ABC";
}

//stubbing
define when call sayABC the return "CBA"

//act
sayABC();   //call then we got "CBA" not "ABC"

原文参考:Stub - an object with hardcoded behavior suitable for a given test (or a group of tests)

Spy : mock的代理对象,当方法被stub的时候,调用stub的定义行为(方法实现被忽略);当方法没有被stub时,调用真实对象的行为(调用真实方法的逻辑实现)。此对象一般不由mock生成,而是自己编码new,再通过spy包装成mock

原文参考:Spy - a mock created as a proxy to an existing real object; some methods can be stubbed, while the un-stubbed ones are forwarded to the covered object

Test Doubles:测试替身,包括5个类型stub,mock,spy,fake,dummy。


地心历险(待研究)

揭示底层的原理


实战

1. 概述

编写测试用例,一般分为三个阶段:

Section name Responsibility
arrange (given) SUT and mocks initialization and configuration
act (when) An operation which is a subject to testing;preferably only one operation on an SUT
assert (then) The assertion and verification phase

arrange-act-assert模式对应的语法为:when().thenXXX()
given-when-then模式(对应BDD形式)对应的语法为:given().willXXX()
官方大部分DEMO使用given-when-then模式,而且也推荐使用这种模式

官方原文: given-when-then comments make intentions of tests clearer.

2. simple example

import org.testng.annotations.Test;
import static org.mockito.Mockito.*;
import static org.testng.Assert.assertEquals;

public class SimpleStubbingTest {
    public static final int TEST_NUMBER_OF_LEAFS = 5;
    
    @Test
    public void shouldReturnGivenValue() {
        // arrange
        Flower flowerMock = mock(Flower.class);
        when(flowerMock.getNumberOfLeafs()).thenReturn(TEST_NUMBER_OF_LEAFS);
    
        // act
        int numberOfLeafs = flowerMock.getNumberOfLeafs();
        
        // assert
        assertEquals(numberOfLeafs, TEST_NUMBER_OF_LEAFS);
    }

}
import static org.mockito.Mockito.*;
import static org.mockito.BDDMockito.*;
import static org.junit.Assert.*; 

public class SimpleStubbingTest {
    public static final int TEST_NUMBER_OF_LEAFS 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值