1

我知道有很多关于 roboelectric 和 gradle 的帖子,但我无法为我的项目设置它。

让我们从情况开始。

我的项目有三种构建类型,每种构建类型使用不同的包名称。我正在使用一些需要在清单中使用包名称的 SDK,所以为了解决这个问题,我在清单中使用了一个键,然后在每个 buildType 中我使用它

manifestPlaceholders = [_MY_PACKAGENAME_: getName()]

因此,我按照 gradle 指南中的说明配置了 roboelectirc,但我无法使其工作。我尝试了不同的东西,例如插件 Android Unit Test 和所有的解决方案,我得到了不同的错误。

喜欢java.lang.NoSuchMethodError cannot be cast to java.lang.RuntimeException

任何人都可以帮助我吗?

测试示例代码:

@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class RoboElectricVerifyTest {

    @Test
    public void simpleTest() throws Exception {
        final Context context = Robolectric.getShadowApplication().getApplicationContext();
        System.out.println("Package name is: " + context.getPackageName());

        Activity activity = Robolectric.buildActivity(MainActivity.class).create().get();
        assertThat(activity != null);
    }

}

这是输出

WARNING: no system properties value for ro.build.date.utc
DEBUG: Loading resources for com.package.name from /Users/path_to_project/app/build/test-resources/Debug/res...
WARNING: no system properties value for gsm.sim.operator.alpha
Unexpected HTTP call POST https://07f605c7.api.splkmobile.com/1.0/07f605c7/0/0/1?hash=none HTTP/1.1
DEBUG: Loading resources for android from jar:/Users/marcel/.m2/repository/org/robolectric/android-all/4.3_r2-robolectric-0/android-all-4.3_r2-robolectric-0.jar!/res...

java.lang.ClassCastException: java.lang.NoSuchMethodError cannot be cast to java.lang.RuntimeException
    at org.robolectric.internal.ReflectionHelpers.callInstanceMethodReflectively(ReflectionHelpers.java:68)
    at org.robolectric.util.ActivityController$1.run(ActivityController.java:115)
    at org.robolectric.shadows.ShadowLooper.runPaused(ShadowLooper.java:268)
    at org.robolectric.util.ActivityController.create(ActivityController.java:111)
    at org.robolectric.util.ActivityController.create(ActivityController.java:122)
    at package.RoboElectricVerifyTest.simpleTest(RoboElectricVerifyTest.java:25)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.robolectric.RobolectricTestRunner$2.evaluate(RobolectricTestRunner.java:236)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.robolectric.RobolectricTestRunner$1.evaluate(RobolectricTestRunner.java:158)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:74)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:211)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:67)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)


Process finished with exit code 255
4

0 回答 0