不可能的RuntimeException:使用Robolectric存根

时间:2013-03-24 20:25:51

标签: android robolectric

我花了好几个小时试图解决这个问题。

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

  

java.lang.RuntimeException:Stub!       在android.net.Uri。$$ robo $$ Uri_30fc_parse(Uri.java:53)       at 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)       at 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,只是在模拟器中运行应用程序进行测试。

1 个答案:

答案 0 :(得分:3)

我和你有同样的问题。还有一个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