美文网首页
Mock and Stub

Mock and Stub

作者: 星期六1111 | 来源:发表于2017-09-03 20:07 被阅读14次

description:

We have a dojo about Mock and Stub.But first we couldn't use mockito to do that and need to use to a fack class to finsh. In the class CashRegister, it has a method process.In this method, we need to call the other method print which in the class Print. At the same time, we need to call the method asString,which in other class Purchase.

Question

  • Q1: if don't use mockito, I don't know how to test the print method .
    A1: My India pair told me that we should create a class FakePrint and FakePurchaseto simulate classes.Why ? I think What we have to do is like mokito, need a fake FakePrintto show that the method printhas been called, and FakePurchase to show the method asStringhas return something.

  • Q2: what the differences between the two tests?
    A2: We test Whether print the method is called,it's Mock.Mock concerned about behavior verification; While we test the value of asStringreturned. It 's Stub.Stub focus on the state verification,For replacing a method with code that returns a specified result. As the book 《Practical Unit Testing with JUnit and Mockito》said, Mock used to verify if the SUT(System under test) calls specific methods of the collaborator ("indirect outputs"), while used for passing some values to the SUT ("indirect inputs").

  • Q3: Why we use Mock and Stub?
    A3:A unit test should test functionality in isolation. Side effects from other classes or the system should be eliminated for a unit test, if possible.

  • Q4:When we use Mock and when we use Stub?
    A4: When the behavior of the objects we simulate is uncertain, it is difficult to create and simulate, or the real object is not yet implemented,we use Mock,for example: network error. Stubs are typically encapsulated for a real object,Such as access to file systems, database connections, and remote protocols.

Reflection

sometimes,I know some concept, but I don't know how to use them.

action

When we look at the document to learn new things, according to the example to write demo again, do not just focus on the concept itself.

相关文章

  • 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)

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

网友评论

      本文标题:Mock and Stub

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