Java 中的模拟对象是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2128148/
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-13 03:51:53  来源:igfitidea点击:

What are mock objects in Java?

javaunit-testingmocking

提问by giri

I like to know what mock objectsare in Java. Why do we create them and what are their uses?

我想知道Java 中的模拟对象是什么。我们为什么要创建它们以及它们的用途是什么?

回答by danben

A Mock object is something used for unit testing. If you have an object whose methods you want to test, and those methods depend on some other object, you create a mock of the dependency rather than an actual instance of that dependency. This allows you to test your object in isolation.

Mock 对象是用于单元测试的东西。如果您有一个要测试其方法的对象,并且这些方法依赖于某个其他对象,则您可以创建依赖项的模拟,而不是该依赖项的实际实例。这允许您单独测试您的对象。

Common Java frameworks for creating mock objects include JMockand EasyMock. They generally allow you to create mock objects whose behavior you can define, so you know exactly what to expect (as far as return values and side effects) when you call methods on the mock object.

用于创建模拟对象的常见 Java 框架包括JMockEasyMock。它们通常允许您创建可以定义其行为的模拟对象,因此当您在模拟对象上调用方法时,您确切地知道会发生什么(就返回值和副作用而言)。

As an example, one common use case might be in an MVC application, where you have a DAO layer (data access objects) and a Controller that performs business logic. If you'd like to unit test the controller, and the controller has a dependency on a DAO, you can make a mock of the DAO that will return dummy objects to your controller.

例如,一个常见的用例可能是在 MVC 应用程序中,您有一个 DAO 层(数据访问对象)和一个执行业务逻辑的控制器。如果您想对控制器进行单元测试,并且控制器依赖于 DAO,您可以模拟 DAO,将虚拟对象返回给您的控制器。

One thing thats important to note is that its usually the case that mock objects implement the same interface as the objects that they are mocking - this allows your code to deal with them via the interface type, as if they are instances of the real thing.

需要注意的一件事是,mock 对象通常实现与它们正在模拟的对象相同的接口 - 这允许您的代码通过接口类型处理它们,就好像它们是真实事物的实例一样。

回答by GuruKulki

Mock objects are the one which is used in unit testing. which helps you to test the only functionality you want to test. because all the dependencies can be achieved by mocking those dependent objects. So if the test passes you will be sure that your unit under test is correct.

模拟对象是用于单元测试的对象。这可以帮助您测试您想要测试的唯一功能。因为所有的依赖都可以通过模拟那些依赖对象来实现。因此,如果测试通过,您将确保被测单元是正确的。

So its the test writer who creates them. You can use EasyMock as one the tool for Mocking.

所以它是创建它们的测试作者。您可以使用 EasyMock 作为 Mocking 的工具之一。

回答by davek

Quote from here: http://easymock.org/

从这里引用:http: //easymock.org/

Unit testing is the testing of software units in isolation. However, most units do not work alone, but they collaborate with other units. To test a unit in isolation, we have to simulate the collaborators in the test. A Mock Object is a test-oriented replacement for a collaborator. It is configured to simulate the object that it replaces in a simple way.

单元测试是孤立地测试软件单元。然而,大多数单位不是单独工作,而是与其他单位合作。为了单独测试一个单元,我们必须模拟测试中的合作者。模拟对象是协作者的面向测试的替代品。它被配置为以简单的方式模拟它替换的对象。

for example, if you are wanting to test http calls you will need to create real-life request objects,complete with all their dependencies. This can often require a lot of effort, hence the use of mocked objects which provide a quicker path to creating an accurate rendition of the object you need, without the long chain of dependencies.

例如,如果您想测试 http 调用,您将需要创建现实生活中的请求对象,并完成它们的所有依赖项。这通常需要付出很多努力,因此使用模拟对象可以更快地创建所需对象的准确再现,而无需长链依赖关系。

回答by Kaleb Brasee

Mock objects let you simulate and verify real objects, without actually running the real code in those objects. You can set up a mock to return specific results on method calls, you can verify that a method was or wasn't called, and other cool stuff.

Mock 对象让您可以模拟和验证真实对象,而无需在这些对象中实际运行真实代码。你可以设置一个模拟来返回方法调用的特定结果,你可以验证一个方法是否被调用,以及其他很酷的东西。

Mockitois a very simple and straightforward Java mock object library.

Mockito是一个非常简单直接的 Java 模拟对象库。

回答by sateesh

Mocking and Mock Objects is not specific to Java. Mock objects is a unit testing technique in which a code chunk is replaced by dummy implementations that emulate real code. This helps one to write unit tests targeting the functionality provided by the class under test.

Mocking 和 Mock 对象并不是 Java 特有的。模拟对象是一种单元测试技术,其中代码块被模拟真实代码的虚拟实现替换。这有助于编写针对被测类提供的功能的单元测试。

Check these articles which provide a very good introduction to the concept of mocking:
Mock Objects(PDF)
Endo-Testing: Unit Testing with Mock Objects(PDF)

查看这些文章,它们很好地介绍了
模拟的概念:模拟对象(PDF)
Endo-Testing: Unit Testing with Mock Objects(PDF)

If you are looking for a mock framework for unit testing in Java, have a look at: Mockito. I have found it useful for my unit tests.

如果您正在寻找用于 Java 单元测试的模拟框架,请查看: Mockito。我发现它对我的单元测试很有用。