android - 如何在 Android Studio 中运行 Robolectric?

标签 android junit robolectric

我花了几个小时尝试在 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:

  1. Remove everything except -classpath “…”
  2. Move jUnit 4 to be the first thing in the classpath
  3. 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/

相关文章:

android - 如何使用 robolectric 覆盖注入(inject)的 @Singleton 类?

unit-testing - 片段的Robolectric单元测试间歇性地失败,并带有ClassNotFoundException异常

android - 完成一项 Activity 时跳过后面堆栈中的项目?

android - Lotus Notes Traveler 应用程序中的 Intent.ACTION_SEND 问题

java - 查找被忽略的 junits 的程序

java - Mockito 单元测试 RestTemplate

java - 如何在同一方法中模拟第二个对象?

android - getSupportActionBar() 使用 Robolectric 返回 null

android - 内存分析器 (MAT) 插件未在 MAT Eclipse 4.1.2 中自动打开堆教授

android - 如何使用 libjpeg-turbo 解码 JPEG9 图像?