开发者

mock tests in android [closed]

开发者 https://www.devze.com 2022-12-14 14:00 出处:网络
开发者_如何转开发 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current fo
开发者_如何转开发 It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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


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.

0

精彩评论

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