【问题标题】:Impossible RuntimeException: Stub with Robolectric不可能的 RuntimeException:带有 Robolectric 的存根
【发布时间】:2013-03-24 20:25:50
【问题描述】:

我已经花了几个小时试图弄清楚这一点。

我正在使用 Maven 在 IntelliJ IDEA 中使用 Robolectric 构建和测试一个项目。我在 POM 中的 Android 之前声明了 Robolectric,并且我使用 SDK 部署程序安装到我的本地存储库。但我仍然不断收到此错误:

java.lang.RuntimeException:存根! 在 android.net.Uri.$$robo$$Uri_30fc_parse(Uri.java:53) 在 org.robolectric.bytecode.ShadowWrangler$InvocationPlan.callOriginal(ShadowWrangler.java:591) 在 android.net.Uri.parse(Uri.java) 在 org.robolectric.shadows.ShadowMediaStore.reset(ShadowMediaStore.java:27) 在 org.robolectric.Robolectric.resetStaticState(Robolectric.java:821) 在 org.robolectric.RobolectricTestRunner.resetStaticState(RobolectricTestRunner.java:224) 在 org.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:133) 在 org.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:96) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68) 在 org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47) 在 org.junit.runners.ParentRunner$3.run(ParentRunner.java:231) 在 org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60) 在 org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229) 在 org.junit.runners.ParentRunner.access$000(ParentRunner.java:50) 在 org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222) 在 org.junit.runners.ParentRunner.run(ParentRunner.java:300) 在 org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264) 在 org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) 在 org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124) 在 org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:208) 在 org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:158) 在 org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86) 在 org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153) 在 org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:95)

而我的 pom.xml 正好如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.jemson</groupId>
<artifactId>notecloud-mobile</artifactId>
<version>.01</version>
<packaging>apk</packaging>
<name>Note-Cloud Mobile</name>

<profiles>
    <profile>
        <id>offline</id>
        <properties>
            <config.scheme>http://</config.scheme>
            <config.host>10.0.2.2</config.host>
            <config.path>/notecloudtest/index.html</config.path>
        </properties>
    </profile>
    <profile>
        <id>online</id>
        <properties>
            <config.scheme></config.scheme>
            <config.host></config.host>
            <config.path></config.path>
        </properties>

    </profile>
</profiles>

<dependencies>
    <dependency>
        <groupId>com.logician</groupId>
        <artifactId>abstractmodel</artifactId>
        <version>1</version>
    </dependency>
    <dependency>
        <groupId>com.androidquery</groupId>
        <artifactId>android-query</artifactId>
        <version>0.24.3</version>
    </dependency>

    <dependency>
        <groupId>org.robolectric</groupId>
        <artifactId>robolectric</artifactId>
        <version>2.0-alpha-2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>org.hamcrest</groupId>
        <artifactId>hamcrest-core</artifactId>
        <version>1.2</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.10</version>
        <scope>test</scope>
    </dependency>

    <dependency>
        <groupId>com.googlecode.androidannotations</groupId>
        <artifactId>androidannotations</artifactId>
        <version>2.7</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>com.googlecode.androidannotations</groupId>
        <artifactId>androidannotations-api</artifactId>
        <version>2.7</version>
    </dependency>


    <dependency>
        <groupId>android</groupId>
        <artifactId>android</artifactId>
        <version>4.2.2_r2</version>
        <scope>provided</scope>
    </dependency>

    <dependency>
        <groupId>android.support</groupId>
        <artifactId>compatibility-v4</artifactId>
        <version>12</version>
    </dependency>

</dependencies>


<build>
    <finalName>${project.artifactId}</finalName>
        <plugins>

            <plugin>
                <groupId>com.google.code.maven-replacer-plugin</groupId>
                <artifactId>replacer</artifactId>
                <version>1.5.2</version>
                <executions>
                    <execution>
                        <phase>prepare-package</phase>
                        <goals>
                            <goal>replace</goal>
                        </goals>
                    </execution>
                </executions>
                <configuration>
                    <file>templates/Config.java</file>
                    <outputFile>src/main/java/org/jemson/notecloud/Config.java</outputFile>
                    <replacements>
                        <replacement>
                            <token>#scheme</token>
                            <value>${config.scheme}</value>
                        </replacement>
                        <replacement>
                            <token>#host</token>
                            <value>${config.host}</value>
                        </replacement>
                        <replacement>
                            <token>#path</token>
                            <value>${config.path}</value>
                        </replacement>
                    </replacements>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <!-- See http://code.google.com/p/maven-android-plugin/ -->
                <groupId>com.jayway.maven.plugins.android.generation2</groupId>
                <artifactId>android-maven-plugin</artifactId>
                <version>3.4.1</version>
                <configuration>
                    <sdk>
                        <platform>17</platform>
                    </sdk>
                </configuration>
                <extensions>true</extensions>
            </plugin>


        </plugins>
</build>

AbstractModel 是我自己的个人便利库。它在内部的任何地方都不使用 Uri 类。至于Android-Query,我不确定。我认为对 Uri.parse() 的有问题的调用在我正在测试的 Activity 类中;我已经尝试评论调用以查看会发生什么,但仍然抛出异常。

我尝试在 IntelliJ 模块配置中移动 Robolectric 依赖项,以确保它们实际上是在 Android 之前包含的。我已将我的 POM 和 .iml 文件与 RobolectricSample 项目文件尽可能匹配。我什至删除了项目树中的所有 IntelliJ 文件,并从 POM 中重新打开了项目。什么都没有。

我完全糊涂了。我已经尝试了我能想到的一切。我正在考虑为这个项目取消 Robolectric,并在模拟器中运行应用程序进行测试。

【问题讨论】:

    标签: android robolectric


    【解决方案1】:

    我和你有同样的问题。还有一个additional

    无用的文字剪切

    更新 1。

    在 github 上发现了这个问题: https://github.com/pivotal/robolectric/issues/426

    有些人说在 mac os 上可以工作..

    更新 2。

    找到解决方法(esam091 评论):

    public class YourTestRunner extends RobolectricTestRunner
    {
        public YourTestRunner(Class<?> testClass) throws InitializationError
        {
            super(RobolectricContext.bootstrap(YourTestRunner.class, testClass,
                new RobolectricContext.Factory()
                {
                    @Override
                    public RobolectricContext create()
                    {
                        return new RobolectricContext()
                        {
                            @Override
                            public boolean useAsm() // this override does the trick
                            {
                                return false;
                            }
    
                            @Override
                            protected AndroidManifest createAppManifest()
                            {
                                return new AndroidManifest(
                                    new File("YourApp/src/main/android/AndroidManifest.xml"),
                                    new File("YourApp/src/main/android/resources"));
                            }
                        };
                    }
                }));
        }
    
        @Override
        public void prepareTest(Object test)
        {
            RoboGuice.injectMembers(Robolectric.application, test);
        }
    }
    

    在每个使用 Android API 的测试中指定 @RunWith(YourTestRunner.class)

    更新 3。

    已解决的问题: https://github.com/pivotal/robolectric/pull/458

    【讨论】:

    • 是的,我在发布问题后不久就发现了这一点。我正在考虑实施它,但我认为实施时间不会与使用单元测试而不是使用模拟器手动测试所节省的时间相平衡。也许在我完成这个项目之后。
    猜你喜欢
    • 2012-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-02
    • 2017-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多