开发者

What are mock objects in Java?

开发者 https://www.devze.com 2022-12-18 10:17 出处:网络
I like to know what mock objects are in Java. Why do we create them and what ar开发者_JAVA技巧e their uses?A Mock object is something used for unit testing.If you have an object whose methods you want

I like to know what mock objects are in Java. Why do we create them and what ar开发者_JAVA技巧e their uses?


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.

Common Java frameworks for creating mock objects include JMock and 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.

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.

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.


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.

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)

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.


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.

Mockito is a very simple and straightforward Java mock object library.


Quote from here: 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消