Reputation: 27199
I like to know what mock objects are in Java. Why do we create them and what are their uses?
Upvotes: 53
Views: 82372
Reputation: 1
Suppose Ramesh and Suresh is working on a project. And Ramesh is developing one method to save the client details and Suresh is developing another method which will validate client exist or not in the DB if it will not exist then only it will return false and then only client will be saved.
So here save method depends on the output of isExist method and if you want to test the save method then you need object which has isExist method but the problem is isExist method is not fully developed then how will you test.
Then you will create Mock Object of that class which contain isExist method and then you will be able to test your save method.
So we can say that Mock object is Pseudo Object of any class which help you run the test cases of your unit.
Hence we will not create real object but Mock Object to test any method .
Upvotes: 0
Reputation: 28663
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.
Upvotes: 16
Reputation: 26418
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.
Upvotes: 1
Reputation: 51925
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.
Upvotes: 5
Reputation: 22905
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.
Upvotes: 4
Reputation: 83250
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.
Upvotes: 69