开发者

How to execute Java tests on Android code

开发者 https://www.devze.com 2023-03-31 17:31 出处:网络
I want to execute a Java class to test pieces of an Android application\'s functionality (from a Java Module) to read/write data to file. If I execute within the Android module, I get Stub! errors (An

I want to execute a Java class to test pieces of an Android application's functionality (from a Java Module) to read/write data to file. If I execute within the Android module, I get Stub! errors (Android SDK). If I install or emulate, it is very slow (I'm only testing a specific piece).

If I include the Android module as a Module Dependency, IntelliJ still builds the Android files and I get a Stub! error during execution. But if I only include the classes directory and mark them as Provided, I get errors when Log. is called.

Is there a way to run simple Java tests on components of an Android application (not using Activity, etc), us开发者_JS百科ing the Java SDK and not building Android specific files?


You need to either instrument your tests and run them on a device (or emulator), or use Roboelectric, it provides replacements for the SDK jar stubbed classes.


Seems to be fixed in later releases of IntelliJ 10 - hadn't been able to reproduce the issue


While you can use Robolectric or other techniques to tests on the host JVM, it's worth noticing that this should only be used as a workaround during the development cycle to avoid long waits for the tests to run on the emulator or device and would never replace testing on the real platform as incompatibilities between Dalvik and JavaSE runtime may affect the accuracy of the tests.

Latest devices, introducing more powerful CPUs, can run the tests at a very reasonable speed in most of the cases, so probably there's no need to hunt for other solutions.

0

精彩评论

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