tags:

views:

188

answers:

1

what is mock testing..How can we do mock testing..how to create mocks..when to use it

+2  A: 

Mocks basically allow you to replace a concrete implementation of a class with a "fake" instance to simulate specific scenarios that you want to test.

For example, suppose you have ClassA.DoSomething() method that accepts a ClassB instance as a parameter and uses it to do some work. You may want to test what happens in specific scenarios when ClassB is used within ClassA.DoSomething() (for example, what happens when ClassB.HelperMethod() is called and it returns a null).

You can supply a "mocked" (fake) instance of ClassB, and tell it exactly how to behave from your test. For example, you would tell it to expect a call to "HelperMethod" and to return a null value for that call.

Mocks are useful for allowing you to test individual units without relying on external dependencies. By mocking the external component, you remove the dependency on it from your tests and it means you focus your unit testing on your discrete component instead of doing integration testing which tests the integration between 2 components. A good example is a class that uses a data access layer to retrieve data from a database and then does some manipulation on the data. You can mock the data access layer to prevent the need from actually needing to go off to the database. So you can focus on testing how the class works rather than having to focus on setting up the db with all required test data and testing the integration with the DB. This has the added benefit of speeding up your tests too.

Wikipedia has a good overview of mocking
Android documentation on support for mocks.

AdaTheDev
thanks for the help..
Bharat Pawar
pls give up votes if u like the answer
Bohemian
...and if it answers your question for you, accept it :)
AdaTheDev