开发者

Mocking library/framework that works best in Android? [closed]

开发者 https://www.devze.com 2023-01-09 03:15 出处:网络
Closed. This question is opinion-based. It is not currently accepting answers. Want to improve this question? Update the question so it can be answered with facts and citations by editing
Closed. This question is opinion-based. It is not currently accepting answers.

Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.

Closed 3 years ago.

Improve this question

I'm developing Android application using third party libraries (Twitter4j). I want to be able mock those objects (also objects created by me) in JUnit and 开发者_如何学运维functional tests.

Do you have any good experiences using some mocking libraries and you can recommend them?


(Update: Mockito has added Android support as of version 1.9.5 and EasyMock has added Android support as of version 3.2 by factoring out those bits that generate code at runtime and making them pluggable, e.g. by using dexmaker instead of cglib.)

Except for android-mock mentioned by DixonD (which is a fairly young, unproven library), there currently is no solution. You can immediately forget anything based on CGLib (Mockito, plain EasyMock), since CGLib relies on byte code generation and won't work on Dalvik (it also relies on the Java Beans package, which is also not part of Android).

For what it's worth, you could use the very few mock classes coming with Android (like MockContext), but they don't verify behavior, they're just stubs. Their default behavior is to throw a runtime error in every method, so you have to subclass them and override the methods you want to mock.

However, you can still use mocking libraries in non-instrumentation tests, i.e. in your standard unit tests executed on the JVM. You can use PowerMock to mock framework methods, it has support for mocking static methods and constructors, making the mocking as powerful as e.g. in Ruby (just more painful to use).

We use JUnit 4 + PowerMock + Mockito and mock out classes like Context and TextUtils in a base class from which we inherit every normal JUnit test. For instrumentation tests, we create custom mock classes and decide using a factory which implementation (mock or not) to instantiate at runtime.


I recently released Borachio, a native Scala mocking framework which works on Android.

Because Borachio is written in Scala, you’ll need to write your tests in Scala. But it can be used to test code written in Java.

There's a description of how to use Borachio on Android on my blog:

http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-1/ http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-2/ http://www.paulbutcher.com/2011/03/mock-objects-on-android-with-borachio-part-3/

UPDATE:

Borachio now is ScalaMock.


Robolectric uses a different approach. Instead of running on the DVM, it "defangs" the Android SDK so that you can run Android tests directly on the JVM with the JUnit4 framework. Tests apparently build and run much faster, and require less mocking.

[A common approach] is to use mock frameworks such as Mockito or Android Mock to mock out the Android SDK. While this is a valid approach, we have found that without Robolectric, the level of mocking needed to test an Android app quickly yields tests that are essentially reverse implementations of the application code.

Robolectric allows a test style that is closer to black box testing, making the tests more effective for refactoring and allowing the tests to focus on the behavior of the application instead of the implementation of Android. You can still use a mocking framework along with Robolectric if you like.

Here's how it works:

[It intercepts] the loading of the Android classes and rewriting the method bodies. Robolectric re-defines Android methods so they return null (or 0, false, etc.), or if provided Robolectric will forward method calls to shadow Android objects giving the Android SDK behavior.

A comparison to Mockito.


Android Mock is written on top of EasyMock 2.4 which is well-known mock framework for Java


Lmock is working on Android : github.com/vmware/lmock


update: looks like easymock 3.2 added an option for plugging-in alternatives for cglib.

I'm using easymock 2.5.2 (note - do not use 3.X). it works - but only for mocking interfaces.

Thus if your library exposes interfaces or if you're willing to wrap our dependencies with interfaces you can use easymock.

Later easymock versions such as easymock 3.x won't work because they use the android-incompatible cglib for bytecode manipulation for both classes and interfaces while 2.x uses it only for mocking classes.


I have just tried Android-Mock. It works very well so far. It Resolved my issue (either use AndroidTestCase without EasyMock, or use EasyMock but no context allowed)

0

精彩评论

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