Mock & Stub (JUnit)

作者: Pursue | 来源:发表于2016-05-30 21:27 被阅读102次

Visit This Article In Github Page

Abstract

Both mock and stub are mummy objects for unit test in spring.When you have lots of dependencies in unit test, creating fake object to reduce dependency is really recommended. Therefore, we use mock and stub. But there are some differences between mock and stub.

Stub

Stub is a common way to use without extra dependency in unit test.It trys to describe the behevior of the method, So we just concern about the return value when use stub.

Here is example:


class CashRegister {
    public void process(Purchase purchase, Printer printer) {
        printer.print(purchase.asString());
    }
}

Now we have a method in the instance of the CashRegister, It have purchase and printer so that It can print the bill when invoke process.

But it is not realistic for us to use a real printer in our test, so we try to use a fake printer to do unit test.

In stub approach:

We create a sub printer


public class FakePrinter extends Printer {
    public boolean wasInvoked;
    @Override
    public void print(String printThis) {
        wasInvoked = true;
    }
}

We test "if printer is invoked when process"


@Test
public void shouldPrintInfoOfPurchase() throws Exception {
    FakePrinter fakePrinter = new FakePrinter();
    Item[] items = {
        new Item("xiaofei", 200.00)
    };
    CashRegister cashRegister = new CashRegister();
    Purchase purchase = new Purchase(items);
    cashRegister.process(purchase, fakePrinter);
    assertTrue(fakePrinter.wasInvoked);
}

Mock

Mock is similar with stub, but mock is a real fake object.

We can test the above method as:


@Test
public void shouldPrintInfoOfPurchaseWithMockPrinter() throws Exception {
    Printer fakePrinter = Mockito.mock(Printer.class);
    Item[] items = {
        new Item("xiaofei", 200.00)
    };
    CashRegister cashRegister = new CashRegister();
    Purchase purchase = new Purchase(items);
    cashRegister.process(purchase, fakePrinter);
    verify(fakePrinter).print(purchase.asString());
}

By using the framework of Mockito, we can create a fake object by Class, and the verify assertion can check if the method has been invoked.

Summary

According to Martin Fowler's article:

  • Dummy objects are passed around but never actually used. Usually they are just used to fill parameter lists.

  • Fake objects actually have working implementations, but usually take some shortcut which makes them not suitable for production (an in memory database is a good example).

  • Stubs provide canned answers to calls made during the test, usually not responding at all to anything outside what's programmed in for the test. Stubs may also record information about calls, such as an email gateway stub that remembers the messages it 'sent', or maybe only how many messages it 'sent'.

  • Mocks are what we are talking about here: objects pre-programmed with expectations which form a specification of the calls they are expected to receive.

相关文章

  • Mock & Stub (JUnit)

    Visit This Article In Github Page Abstract Both mock and ...

  • Mock and Stub

    description: We have a dojo about Mock and Stub.But first...

  • 【JAVA UT】15、mock与stub的比较

    文|码术张 本节通过比较stub与mock,加深对stub、mock的认识。 1、相同点 都是在ut中消除依赖的一...

  • Java单元测试

    概念 Stub和Mock 为什么使用Stub或者Mock? 因为要测试的对象通常会依赖于其他对象,而我们并不需要测...

  • Mock与Stub

    Mock验证行为,Stub验证状态。但需要细分一下场景: 1. 需要test double提供输入时,state也...

  • php单元测试进阶(13)- 核心技术 - mock对象 - 同

    php单元测试进阶(13)- 核心技术 - mock对象 - 同时使用mock和stub 本系列文章主要代码与文字...

  • 微服务实战之Mock

    模拟对象 一般都叫 Mock 或 Stub, 两者差不多, 都是模拟被测组件对外依赖的模拟, 存根 stub 就在...

  • Mockito入门

    mock使用 mock主要在单元测试的时候用来模拟外部依赖接口的返回,即method stub的作用。 一般而言,...

  • Mockito入门和原理初探

    mock使用 mock主要在单元测试的时候用来模拟外部依赖接口的返回,即method stub的作用。 一般而言,...

  • 置换测试:Stub,Mock

    从测试的角度看,理想情况下,我们的所作的全部测试都是对应了实际的代码,但这并不适用于实际情况,比如每次测试都去访问...

网友评论

    本文标题: Mock & Stub (JUnit)

    本文链接:https://www.haomeiwen.com/subject/zarcdttx.html