Impossible RuntimeException: Stub with Robolectric

纵然是瞬间 提交于 2019-12-02 04:52:54

问题


I've spent hours trying to figure this out.

I'm using Maven to build and test a project in IntelliJ IDEA with Robolectric. I have Robolectric declared before Android in the POM, and I've used the SDK deployer to install to my local repo. But I still keep getting this error:

java.lang.RuntimeException: Stub! at android.net.Uri.$$robo$$Uri_30fc_parse(Uri.java:53) at org.robolectric.bytecode.ShadowWrangler$InvocationPlan.callOriginal(ShadowWrangler.java:591) at android.net.Uri.parse(Uri.java) at org.robolectric.shadows.ShadowMediaStore.reset(ShadowMediaStore.java:27) at org.robolectric.Robolectric.resetStaticState(Robolectric.java:821) at org.robolectric.RobolectricTestRunner.resetStaticState(RobolectricTestRunner.java:224) at org.robolectric.RobolectricTestRunner.internalBeforeTest(RobolectricTestRunner.java:133) at org.robolectric.RobolectricTestRunner.methodBlock(RobolectricTestRunner.java:96) 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.junit.runners.ParentRunner.run(ParentRunner.java:300) at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264) at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153) at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124) at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray2(ReflectionUtils.java:208) at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:158) at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:86) at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153) at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:95)

And my pom.xml is exactly as follows:

<?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 is my own personal convenience library. It does not use the Uri class anywhere internally. As for Android-Query, I'm not sure. What I think is the problematic call to Uri.parse() is in the Activity class I'm testing; I've tried commenting the call out to see what would happen but the exception is still thrown.

I've tried moving the Robolectric dependencies around in the IntelliJ module configuration to make sure they are in fact being included before Android. I've matched my POM and .iml files as closely to the RobolectricSample project files as possible. I've even deleted all the IntelliJ files in the project tree and re-opened the project from the POM. Nothing.

I am absolutely flummoxed. I've tried everything I can think of. I'm considering scrapping Robolectric for this project and just running the app in the emulator to test.


回答1:


I'm have the same problem as you. And also one additional.

useless text cut

Update 1.

Found this issue on github: https://github.com/pivotal/robolectric/issues/426

Some peoples talk that on mac os it works..

Update 2.

Found workaround (comment by 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);
    }
}

Specify @RunWith(YourTestRunner.class) on each test which uses Android API.

Update 3.

Issue fixed: https://github.com/pivotal/robolectric/pull/458



来源:https://stackoverflow.com/questions/15603583/impossible-runtimeexception-stub-with-robolectric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!