我决定为我正在开发的android应用程序编写一些单元测试。我选择了Robolectric(版本2.3)。我按照我发现here:
的教程做了@Config(emulateSdk = 18)
@RunWith(RobolectricTestRunner.class)
public class MainTest {
private Main mainActivity;
@Before
public void setup() {
mainActivity = Robolectric.buildActivity(Main.class).create().get();
}
@Test
public void shouldStartNextActivityWhenOnePlayerButtonWasClicked() throws Exception {
Button button = (Button) mainActivity.findViewById(R.id.onePlayer);
button.performClick();
Intent intent = Robolectric.shadowOf(mainActivity).peekNextStartedActivity();
assertThat(intent.getComponent().getClassName()).isEqualTo(NextActivity.class.getCanonicalName());
}
在运行测试期间,我遇到以下错误:
[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:testCompile (default-testCompile) on project app: Compilation failure
[ERROR] \workspace\app\src\test\java\com\example\app\activity\MainTest.java:[36,35] error: cannot access DownloadManager
表示以下行(shadowOf方法)存在问题:
Intent intent = Robolectric.shadowOf(mainActivity).peekNextStartedActivity();
有人可以解释为什么会出现错误以及如何解决错误吗?也许Robolectric不是我做的最佳选择?
EDITED: 我想这个问题可以在我的pom.xml中。我正在使用robolectric(尺寸较小)而不是教程中指出的robolectric-depen-dependencies:
<?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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example.app</groupId>
<artifactId>app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>apk</packaging>
<name>app</name>
<repositories>
<repository>
<id>android-support</id>
<url>file://${env.ANDROID_HOME}/extras/android/m2repository</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.robolectric</groupId>
<artifactId>robolectric</artifactId>
<version>2.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<version>2.8.0</version>
<configuration>
<androidManifestFile>${project.basedir}/AndroidManifest.xml</androidManifestFile>
<assetsDirectory>${project.basedir}/assets</assetsDirectory>
<resourceDirectory>${project.basedir}/res</resourceDirectory>
<nativeLibrariesDirectory>${project.basedir}/src/main/native</nativeLibrariesDirectory>
<sdk>
<platform>10</platform>
</sdk>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>android</id>
<properties>
<android.sdk.path>c:/android/sdk</android.sdk.path>
</properties>
<activation>
<activeByDefault>true</activeByDefault>
</activation>
</profile>
</profiles>
</project>