我花了几个小时试图解决这个问题。
我正在使用 Maven 通过 Robolectric 在 IntelliJ IDEA 中构建和测试项目。我在 POM 中的 Android 之前声明了 Robolectric,并且我已使用 SDK 部署程序安装到我的本地存储库。但我仍然不断收到此错误:
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)
我的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,而只是在模拟器中运行该应用程序进行测试。
最佳答案
我和你有同样的问题。还有一个 additional .
useless text cut
更新 1。
在github上发现这个问题: https://github.com/pivotal/robolectric/issues/426
有些人说它在 macOS 上可以工作..
更新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。
关于android - 不可能的运行时异常 : Stub with Robolectric,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15603583/