标签: robolectric-gradle-plugin

Multidex之后,Robolectric单元测试失败

所以我最近将我的robolectric 3.0升级与我们的主线合并,后者为我们的应用添加了多索支持.不幸的是,这有导致我们的测试失败/不运行的不幸副作用 - 他们只是在第一次尝试安装MultiDex失败后停止,使用此堆栈跟踪:

java.lang.RuntimeException: java.lang.RuntimeException: Multi dex installation failed (/Users/me/Data/Projects/my-android/myapp/. (Is a directory)).
at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:257)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:194)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.runTestClass(JUnitTestClassExecuter.java:86)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassExecuter.execute(JUnitTestClassExecuter.java:49)
at org.gradle.api.internal.tasks.testing.junit.JUnitTestClassProcessor.processTestClass(JUnitTestClassProcessor.java:69)
at org.gradle.api.internal.tasks.testing.SuiteTestClassProcessor.processTestClass(SuiteTestClassProcessor.java:48)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at org.gradle.messaging.dispatch.ContextClassLoaderDispatch.dispatch(ContextClassLoaderDispatch.java:32)
at org.gradle.messaging.dispatch.ProxyDispatchAdapter$DispatchingInvocationHandler.invoke(ProxyDispatchAdapter.java:93)
at com.sun.proxy.$Proxy2.processTestClass(Unknown Source)
at org.gradle.api.internal.tasks.testing.worker.TestWorker.processTestClass(TestWorker.java:105)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:35)
at org.gradle.messaging.dispatch.ReflectionDispatch.dispatch(ReflectionDispatch.java:24)
at …
Run Code Online (Sandbox Code Playgroud)

android gradle robolectric robolectric-gradle-plugin android-multidex

42
推荐指数
1
解决办法
7352
查看次数

ClassCastException:NoClassDefFoundError无法强制转换为RuntimeException

我正在努力将我的代码库升级到Gradle 2.2和Android Studio 1.0.我目前正在尝试让Robolectric 2.4工作,但是当我尝试运行单元测试时,我遇到了一个奇怪的问题.问题只发生在gradle clean; 多次运行测试套件将产生通过测试(如预期的那样).当我在干净后运行测试时,我收到以下错误:

java.lang.ClassCastException: java.lang.NoClassDefFoundError cannot be cast to java.lang.RuntimeException
Run Code Online (Sandbox Code Playgroud)

我已经将错误追溯到此次调用:

Activity activity = Robolectric.setupActivity(MainActivity.class);
Run Code Online (Sandbox Code Playgroud)

我是否使用Robolectric gradle插件(org.robolectric:robolectric-gradle-plugin:0.14.0)或JC和K Android单元测试插件(com.github.jcandksolutions.gradle:android-unit-test:2.1.1)时遇到此错误.

我发现在Robolectric Github上引用了这个问题,但它看起来还没有得到解决:https: //github.com/robolectric/robolectric/issues/1385

这个问题也在android studio单元测试插件中引用,在'Trouble Shooting'下:https: //github.com/evant/android-studio-unit-test-plugin

我目前的示例代码在这里:https: //github.com/KioKrofovitch/robolectric-upgrade-test

我已经能够在Robolectric样本上运行api-android-16项目,但没有看到这个问题,尽管api-android-19和api-android-21项目由于其他原因而失败.我看不出他们做了什么不同的事情,这样他们就不会失败. https://github.com/robolectric/robolectric-samples

有人找到了解决方法吗?对Jenkins或Travis等CI工具进行两次运行测试并不是一个好的解决方法.

编辑:嵌入代码示例

我的顶级build.gradle,我添加了JC和K Unit测试库:

buildscript {
    repositories {
        mavenCentral()
        jcenter()
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:1.0.0'
        classpath 'com.github.jcandksolutions.gradle:android-unit-test:2.1.1'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files …
Run Code Online (Sandbox Code Playgroud)

android gradle robolectric android-gradle-plugin robolectric-gradle-plugin

18
推荐指数
1
解决办法
2411
查看次数

How to add Java 9 to Android Studio?

I am using Robolectric library and the latest version of it v4.3.1 requires Java 9 to run. I am trying to point JRE on edit configurations but I am not finding Java 9 in the drop-down even though I have already installed it. It would be really helpful if somebody can please explain!

在此处输入图片说明

Please check Java 9 installed.

在此处输入图片说明

java android robolectric android-studio robolectric-gradle-plugin

14
推荐指数
3
解决办法
9797
查看次数

Android Studio中无法识别"RobolectricTestRunner.class"

我正在尝试使用Robolectric来运行单元测试,但Android Studio无法识别该类:

@RunWith(RobolectricTestRunner.class)
Run Code Online (Sandbox Code Playgroud)

细节:

classpath 'com.android.tools.build:gradle:3.0.0'
Run Code Online (Sandbox Code Playgroud)

我正在导入依赖:

testImplementation "org.robolectric:robolectric:3.5.1"
Run Code Online (Sandbox Code Playgroud)

和:

android {
    testOptions {
        unitTests {
            includeAndroidResources = true
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它似乎没有出现导入类的选项.我应该添加任何我忘记的依赖项吗?

错误:

 error: package org.robolectric does not exist
 error: package org.robolectric does not exist
 error: package org.robolectric.shadows does not exist
 error: cannot find symbol class RobolectricTestRunner
 error: cannot find symbol variable Robolectric
 error: cannot find symbol variable ShadowApplication
Run Code Online (Sandbox Code Playgroud)

android unit-testing robolectric android-gradle-plugin robolectric-gradle-plugin

11
推荐指数
4
解决办法
2701
查看次数

Android Studio + Robolectric + Gradle Class未找到例外

我从https://github.com/robolectric/deckard-gradle下载了Robolectric deckard-gradle项目并导入到Android Studio.

在我第一次跑步时,我得到了

!!! JUnit version 3.8 or later expected:
    java.lang.RuntimeException: Stub!
      at junit.runner.BaseTestRunner.<init>(BaseTestRunner.java:5)
      at junit.textui.TestRunner.<init>(TestRunner.java:54)
      at junit.textui.TestRunner.<init>(TestRunner.java:48)
      at junit.textui.TestRunner.<init>(TestRunner.java:41)
Run Code Online (Sandbox Code Playgroud)

错误,我从.iml修复此问题.

然后我得到了:

Class Not Found "my test class"
Run Code Online (Sandbox Code Playgroud)

我尝试了几十种解决方案,我在谷歌中发现了这个问题,但没有一个有效.

tdd android robolectric robolectric-gradle-plugin

10
推荐指数
1
解决办法
4793
查看次数

如何解决重复的Gradle依赖关系问题

我试图用robolectric测试我的代码。问题是它有重复的引用。例如

java.lang.RuntimeException: java.lang.RuntimeException: Duplicate class org.apache.maven.artifact.Artifact found in modules maven-ant-tasks-2.1.3.jar (org.apache.maven:maven-ant-tasks:2.1.3) and maven-artifact-2.2.1.jar (org.apache.maven:maven-artifact:2.2.1)
Run Code Online (Sandbox Code Playgroud)

我使用了Gradel工件app:dependencies来获得以下报告。这里是重要的部分:

+--- org.robolectric:robolectric:4.3
|    +--- org.robolectric:annotations:4.3
|    +--- org.robolectric:junit:4.3
|    |    +--- org.robolectric:annotations:4.3
|    |    +--- org.robolectric:sandbox:4.3
|    |    |    +--- org.robolectric:annotations:4.3
|    |    |    +--- org.robolectric:utils:4.3
|    |    |    |    +--- org.robolectric:annotations:4.3
|    |    |    |    +--- org.robolectric:pluginapi:4.3
|    |    |    |    |    +--- org.robolectric:annotations:4.3
|    |    |    |    |    +--- org.apache.ant:ant:1.8.0
|    |    |    |    |    |    \--- org.apache.ant:ant-launcher:1.8.0
|    |    | …
Run Code Online (Sandbox Code Playgroud)

gradle robolectric robolectric-gradle-plugin

10
推荐指数
2
解决办法
432
查看次数

如何在robolectric中使用findViewById()

我只想用robolectric进行测试,看看片段中是否有某个视图可见.我的单元测试看起来像这样:

ActivityController controller = Robolectric.buildActivity(FragmentActivity.class);
FragmentActivity activity = (FragmentActivity) controller.create().start().resume().visible().get();

F fragment = new MyFragment();
activity.getSupportFragmentManager().beginTransaction()
        .add(fragment, FRAGMENT_TAG).commit();


View view = fragment.getView().findViewById(R.id.my_view);
assertEquals(view.getVisibility(), View.VISIBLE);
Run Code Online (Sandbox Code Playgroud)

我正在使用最新的android gradle插件1.1.3,robolectirc 2.4和robolectric gradle插件1.0.1,我的单元测试在test文件夹中(不是androidTest).我无法编译该代码,因为编译器无法解析R.java.

我的build.gradle:

buildscript {
  repositories {
    jcenter()
  }

  dependencies {
    classpath 'com.android.tools.build:gradle:1.1.3'
    classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'
    classpath 'org.robolectric:robolectric-gradle-plugin:1.0.1'
  }
}

apply plugin: 'com.android.library'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'org.robolectric'


android { ... }

dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])

  // Testing
  testCompile 'org.robolectric:robolectric:2.4'
  testCompile 'junit:junit:4.12'

  // …
Run Code Online (Sandbox Code Playgroud)

android robolectric android-gradle-plugin robolectric-gradle-plugin

9
推荐指数
1
解决办法
4483
查看次数

如何在Android模块上运行单元测试?

我有一个具有多个库模块的android项目,我正在尝试测试包含所有模块的特定包.

我试过这个命令:

./gradlew -Dtest.single=com.moduleone* testProductionDebug
Run Code Online (Sandbox Code Playgroud)

它不起作用:它不执行此模块中的测试,而是执行主项目包类中的所有单元测试.

我如何只测试一个模块?

junit android gradle android-gradle-plugin robolectric-gradle-plugin

9
推荐指数
1
解决办法
1223
查看次数

如何使用 Android 依赖项和 robolectric 运行 Junit5 测试

我正在尝试运行这样的单元测试:

    @org.junit.jupiter.api.Test
    void junit5codeCoverage() {
        final int result = new Foo().junit5();

        Assert.assertEquals(Looper.getMainLooper().getThread(), Thread.currentThread());

        assertEquals(-1, result);
    }
Run Code Online (Sandbox Code Playgroud)

这是一个带有 Android 依赖项(即)和 Robolectric的Junit5测试Looper.getMainLooper()

我正在使用mannodermaus 的 junit5 android 插件,它允许在 Android 设置中运行 junit5。但这不是开箱即用的,因为它不会加载 robolectric。所以我尝试了alixwar 的分支,它可以解决 robolectric 和 junit5 测试覆盖率,但仍然不会使用 Android 类。

此外,我还开始研究如何在 junit5 上运行 robolectric 测试,这需要了解其RobolectricTestRunner工作原理以及如何将代码移植到 JUnit5 平台。我对新的 junit5 平台知之甚少,但我发现我可以在运行器之上构建org.junit.platform.runner.JUnitPlatform,以遵循测试运行器概念,这是junit-platform-runner包的一部分。但这与最初的 Junit4 相去甚远,SandBoxTestRunner以至于我无法完成移植。

那么,实现 robolectric junit5 支持的最可行路径是什么,或者我缺少任何(明显的)概念?

junit android robolectric robolectric-gradle-plugin junit5

9
推荐指数
2
解决办法
2169
查看次数

资源$ NotFoundException运行Roboelectric测试时

我刚刚用一个新应用程序设置了Roboelectric 3.2.2,我已经编写了我的第一个简单测试:

@RunWith(RobolectricTestRunner.class)
@Config(manifest="src/main/AndroidManifest.xml",
    packageName="my.pacakge.name.debug")
public class MainActivityTest {

    @Test
    public void clickButton() {
        MainActivity mainActivity = Robolectric.setupActivity(MainActivity.class);
        String text = ((TextView)mainActivity.findViewById(R.id.text_main)).getText().toString();
        assertEquals("Should equal Hello World!", "Hello World!", text);
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经照着所有的设置和说明这里这里,但我仍然得到这个错误每次我尝试运行测试时间:

android.content.res.Resources$NotFoundException: Unable to find resource ID #0x0 in packages [android, my.package.name.debug]
Run Code Online (Sandbox Code Playgroud)

此异常发生在我正在调用的测试的第一行setupActivity().

android robolectric android-studio robolectric-gradle-plugin

7
推荐指数
1
解决办法
1863
查看次数