如何在 JUnit 和 Java 中使用存根?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31890991/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 11:44:39  来源:igfitidea点击:

How to use stubs in JUnit and Java?

javajunitstub

提问by Hamid

I have worked with JUnit and Mocks but I'm wondering, what are the differences between Mocks and Stubs in JUnit and how to use Stubs in JUnit, Java? And as Mocks that have EasyMock, Mockito and so on, what does Stubs uses in Java?

我使用过 JUnit 和 Mocks,但我想知道,JUnit 中的 Mocks 和 Stubs 之间有什么区别,以及如何在 JUnit、Java 中使用 Stubs?而作为拥有 EasyMock、Mockito 等的 Mocks,Stubs 在 Java 中使用的是什么?

Please give some example code for Stubs in Java.

请给出一些 Java 中存根的示例代码。

采纳答案by Sergii Lagutin

To use stubs in junit you don't need any frameworks.

要在 junit 中使用存根,您不需要任何框架。

If you want to stub some interface just implement it:

如果你想存根一些接口,只需实现它:

interface Service {
    String doSomething();
}

class ServiceStub implements Service {
    public String doSomething(){
        return "my stubbed return";
    }
}

Then create new stub object and inject it to tested object.

然后创建新的存根对象并将其注入测试对象。

If you want to stub a concrete class, create subclass and override stubbed methods:

如果你想存根一个具体的类,创建子类并覆盖存根方法:

class Service {
    public String doSomething(){
        // interact with external service
        // make some heavy computation
        return "real result";
    }
}

class ServiceStub extends Service {
    @Override
    public String doSomething(){
        return "stubbed result";
    }
}

回答by Michael Baker

It doesn't matter the framework or technology in my opinion. Mocks and stubs could be defined as follows.

在我看来,框架或技术并不重要。模拟和存根可以定义如下。

A stub is a controllable replacement for an existing dependency (or collaborator) in the system. By using a stub, you can test your code without dealing with the dependency directly.

存根是系统中现有依赖项(或合作者)的可控替代品。通过使用存根,您可以测试代码而无需直接处理依赖项。

A mock object is a fake object in the system that decides whether the unit test has passed or failed. It does so by verifying whether the object under test interacted as expected with the fake object.

模拟对象是系统中的一个假对象,它决定单元测试是通过还是失败。它通过验证被测对象是否按预期与假对象交互来实现。

Perhaps these images can clarify the interactions between a stub an mock.

也许这些图像可以阐明存根和模拟之间的交互。

StubStub

存根存根

MockMock

嘲笑嘲笑

回答by feech

In general - Mock means implement some behavior, stubs - just supply some data. in other words preferable use the work mock when you need to demonstrate that it changes/keeps some state

一般来说 - 模拟意味着实现一些行为,存根 - 只提供一些数据。换句话说,当您需要证明它更改/保持某些状态时,最好使用工作模拟

use the word stub when your classes only expose the internal state. indeed you can use mock everywhere, and stub is just subset of mock

当您的类仅公开内部状态时,请使用 stub 一词。实际上你可以在任何地方使用 mock,而 stub 只是 mock 的一个子集