我花了几个小时尝试在 Android Studio 中制作 Robolectric,但没有成功,我几乎准备放弃它。我收到以下错误:
!!! JUnit version 3.8 or later expected:
java.lang.RuntimeException: Stub!
当然,我使用高于 3.8 的 junit 版本(实际上是 4.11)。
我尝试了与此相关的其他问题中发现的几种技巧,例如:
- 将 junit 依赖项移至某处建议的第一个依赖项,
- 尝试了 gradle 技巧,例如更改 sourceSets.instrumentationtest.setRoot、类路径、依赖项 -> InstrumentTestCompile,应用插件:'android-unit-test',但是 我的 Android Studio 版本 (0.5.7) 中包含的 Gradle 版本没有 似乎认识这些标签。
我什至从 GitHub 下载了示例甚至无法编译。所以我真的厌倦了这个。
有什么想法吗?
最佳答案
This run will fail with a complaint about “JUnit version 3.8 or later expected”. The reason for this is that jUnit 4 is too far down the classpath, and now comes perhaps the least elegant part of this setup: as Kostya Y explains, copy the whole contents of the console with the error message into a text editor and do this:
- Remove everything except -classpath “…”
- Move jUnit 4 to be the first thing in the classpath
- Add an entry for the absolute path to the robolectric test classes directory as the last item in the classpath, in this case /path/to/MyProject/app/build/classes/robolectric.
Now if you try re-running the configuration, you might get an UnsupportedClassVersionError if you’ve previously run the tests in console, or at least this happens to me on my Mac. I don’t know the actual root cause, but if this happens, just do Build > Clean Project to clear out previously built classes.
来源:http://blog.futurice.com/android_unit_testing_in_ides_and_ci_environments
关于android - 如何在 Android Studio 中运行 Robolectric?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23314047/