当前位置: 首页 > 知识库问答 >
问题:

不可能的运行时异常:带有Robolectric的存根

姬自强
2023-03-14

我花了好几个小时想弄明白。

我正在使用Maven用机器人分子技术在IntelliJ IDEA中构建和测试一个项目。在POM中,我在Android之前声明了Robolectric,并使用SDK部署器安装到了我的本地repo。但我还是经常会犯这样的错误:

JAVARuntimeException:存根!在Android。网乌里$$robo$$Uri_30fc_parse(Uri.java:53)位于org。机器人分子。字节码。暗影牧马人计划。android上的callOriginal(ShadowWrangler.java:591)。网乌里。在org上解析(Uri.java)。机器人分子。阴影。影媒体商店。在org上重置(ShadowMediaStore.java:27)。机器人分子。机器人分子。resetStaticState(roblectric.java:821)位于org。机器人分子。机器人分子测试员。resetStaticState(RobolectrictTestRunner.java:224)位于org。机器人分子。机器人分子测试员。org上的internalBeforeTest(RobolectrictTestRunner.java:133)。机器人分子。机器人分子测试员。methodBlock(RobolectrictTestRunner.java:96)位于org。朱尼特。跑步者。BlockJunitClassRunner。org上的runChild(BlockJUnit4ClassRunner.java:68)。朱尼特。跑步者。BlockJunitClassRunner。org上的runChild(BlockJUnit4ClassRunner.java:47)。朱尼特。跑步者。ParentRunner 3美元。在org上运行(ParentRunner.java:231)。朱尼特。跑步者。ParentRunner 1美元。日程安排(ParentRunner.java:60)在org上。朱尼特。跑步者。家长跑步者。org上的runChildren(ParentRunner.java:229)。朱尼特。跑步者。家长跑步者。访问org上的$000(ParentRunner.java:50)。朱尼特。跑步者。ParentRunner 2美元。在org上评估(ParentRunner.java:222)。朱尼特。跑步者。家长跑步者。在org上运行(ParentRunner.java:300)。阿帕奇。专家当然可以。junit4。JUnit4提供程序。在org上执行(JUnit4Provider.java:264)。阿帕奇。专家当然可以。junit4。JUnit4提供程序。org上的executeTestSet(JUnit4Provider.java:153)。阿帕奇。专家当然可以。junit4。JUnit4提供程序。在org上调用(JUnit4Provider.java:124)。阿帕奇。专家当然可以。util。反光片。org上的invokeMethodWithArray2(ReflectionUtils.java:208)。阿帕奇。专家当然可以。布特。ProviderFactory$ProviderProxy。在org上调用(ProviderFactory.java:158)。阿帕奇。专家当然可以。布特。供应商工厂。org上的invokeProvider(ProviderFactory.java:86)。阿帕奇。专家当然可以。布特。叉靴。在org上运行SuitesInProcess(ForkedBooter.java:153)。阿帕奇。专家当然可以。布特。叉靴。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查询,我不确定。我认为对Uri的调用有问题。parse()在我正在测试的活动类中;我试着对调用进行注释,看看会发生什么,但仍然抛出了异常。

我已经尝试在IntelliJ模块配置中移动Robolectrec依赖项,以确保它们确实在Android之前被包括在内。我已经将我的POM和. iml文件尽可能地与RobolectricSample项目文件匹配。我甚至删除了项目树中的所有IntelliJ文件,并从POM中重新打开了项目。一无所获。

我完全不知所措。我已经想尽办法了。我正在考虑在这个项目中放弃Robolectric,而只是在模拟器中运行应用程序进行测试。

共有1个答案

江鹏
2023-03-14

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

无用的文本剪切

更新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的每个测试上指定@RunBy(YourTestRunner.class)

更新3。

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

 类似资料:
  • 问题内容: 我试图在Linux命令提示符下运行JUnit,其中包含必要的JARS(hamcrest- core-1.3.jar和junit.jar)和类文件,并且正在使用以下命令来运行JUnit: TestJunit类: TestRunner: 我在运行此时遇到以下异常 问题答案: 如果您使用 JUnit 4.4核心运行 程序执行一个no的类,则将得到此异常。 请查阅链接以获取更多信息。 由vip

  • 我想知道如何在Eclipse中的源代码包(即/src/resources/images/或/resources/images/)内部或外部的资源文件夹中创建一个具有资源(图片、PDF)的可运行JAR。目前,我的资源在我的eclipse项目的source文件夹中,但我也在包中的它自己的文件夹中尝试过。该程序在eclipse中构建和执行得很好,但当我将其导出为可运行的jar时,在桌面上运行时,我总是得

  • 问题内容: 查看量角器文档,我发现有一个选项可以不使用Selenium服务器使用flag 来运行量角器。 使用selenium服务器和不使用selenium服务器运行量角器测试有什么区别,除了后者仅支持Chrome和Firefox之外? 问题答案: 首先,目前,您有 5种不同的内置选项/方式来连接浏览器驱动程序 : 指定在本地启动Selenium独立服务器 指定连接到正在运行的硒服务器(本地或远程

  • 我已经在 Android 4.3 低功耗蓝牙示例中实现了代码来查找设备。 我的舱单包括: 并且该设备是Nexus 4,已使用出厂4.3映像进行切换。 我曾经让这个东西工作过一次,但在后续运行中,我得到以下错误:

  • 我的项目依赖于scala日志库,我正在尝试升级我的项目以使用dotty。为此,我从https://github.com/lampepfl/dotty-example-project克隆了示例dotty项目并更新了它,包括日志库。请在下面找到项目的目录结构:- 项目/build.properties:- 项目/plugins.sbt 建筑sbt公司 src/main/scala/main。斯卡拉 上

  • 今天我想从SQLCe转到MongoDB。我下载了驱动程序,并在我的电脑上运行本地服务器。 以下是我的实际代码(顺便说一句,我甚至不知道将IMongoCollection转换为MongoCollection是否安全): 但是,每当我试图运行该程序时,就会出现以下错误:“System”类型的未处理异常。木卫一。mscorlib中发生FileNotFoundException。动态链接库 其他信息:无法