如何让 Robolectric 与 Android 版本原型配合使用?
我正在尝试让 Robolectric 使用位于 https 的 Android-release-archetype ://github.com/akquinet/android-archetypes/wiki/Android-release-archetype
到目前为止我运气不好
1. 我已将 Robolectric 依赖项添加到 POM 中。
2.我使用的目标是“全新安装”
然后我得到这样的错误:
[ERROR] /Users/Dewi/Dropbox/mac_projects/a-parent/application-it/src/main/java/dsdf/dsfsdf/a/test/ddd.java:[3,16] package org.junit does not exist
[ERROR] /Users/Dewi/Dropbox/mac_projects/a-parent/application-it/src/main/java/dsdf/dsfsdf/a/test/ddd.java:[4,23] package org.junit.runner does not exist
有人可以尝试让这个原型与 Maven 一起工作并告诉我该怎么做吗? 这是我的测试类:
package dsdf.dsfsdf.a.test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.xtremelabs.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class ddd {
@Test
public void asdasd(){
assertThat(5, equalTo(1));
}
}
这是我的父 POM:
<?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>dsdf.dsfsdf</groupId>
<artifactId>a-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>a - Parent</name>
<modules>
<module>application</module>
<module>application-it</module>
</modules>
<dependencies>
<dependency>
<groupId>com.pivotallabs</groupId>
<artifactId>robolectric</artifactId>
<version>1.0-RC1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android-test</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<version>2.8.3</version>
<inherited>true</inherited>
<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>7</platform>
</sdk>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<inherited>true</inherited>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.1</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.2</version>
<inherited>true</inherited>
<configuration>
<removeExistingSignatures>true</removeExistingSignatures>
<archiveDirectory />
<archive>${project.build.directory}/${project.build.finalName}.${project.packaging}</archive>
<verbose>true</verbose>
<certs>true</certs>
<keystore>${sign.keystore}</keystore>
<alias>${sign.alias}</alias>
<storepass>${sign.storepass}</storepass>
<keypass>${sign.keypass}</keypass>
</configuration>
</plugin>
<plugin>
<groupId>com.pyx4me</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.4</version>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard</artifactId>
<version>4.4</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<configuration>
<proguardVersion>4.4</proguardVersion>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>release</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-signing-properties</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>sign.keystore</property>
<message>The 'sign.keystore' property is missing. It must
contain the path to the keystore used to sign the
application.</message>
</requireProperty>
<requireFilesExist>
<files>
<file>${sign.keystore}</file>
</files>
<message>The 'sign.keystore' property does not point to a
file. It must contain the path to the keystore used to sign
the application.</message>
</requireFilesExist>
<requireProperty>
<property>sign.alias</property>
<message>The 'sign.alias' property is missing. It must
contain the key alias used to sign the application.</message>
</requireProperty>
<requireProperty>
<property>sign.storepass</property>
<message>The 'sign.storepass' property is missing. It must
contain the password of the keystore used to sign the
application.
</message>
</requireProperty>
<requireProperty>
<property>sign.keypass</property>
<message>The 'sign.keypass' property is missing. It must
contain the password of the key used to sign the
application.</message>
</requireProperty>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>linux</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<properties>
<rt.jar.path>${java.home}/jre/lib/rt.jar</rt.jar.path>
<jsse.jar.path>${java.home}/jre/lib/jsse.jar</jsse.jar.path>
</properties>
</profile>
<!-- mac profile has to be after unix since running on mac will trigger
both -->
<profile>
<id>mac</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<properties>
<!-- absolute path -->
<!--<rt.jar.path>/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/classes.jar</rt.jar.path> -->
<!-- or with JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/ -->
<rt.jar.path>${java.home}/../Classes/classes.jar</rt.jar.path>
<jsse.jar.path>${java.home}/../Classes/jsse.jar</jsse.jar.path>
</properties>
</profile>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<rt.jar.path>${java.home}/jre/lib/rt.jar</rt.jar.path>
<jsse.jar.path>${java.home}/jre/lib/jsse.jar</jsse.jar.path>
</properties>
</profile>
</profiles>
</project>
这是子 POM:
<?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>
<parent>
<groupId>dsdf.dsfsdf</groupId>
<artifactId>a-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>dsdf.dsfsdf</groupId>
<artifactId>a-it</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>apk</packaging>
<name>a - Integration tests</name>
<properties>
<!--
When not running a release, we are deploying the plain artifact (so no classifier)
This is overridden during the release.
-->
<zipaligned-classifier></zipaligned-classifier>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android-test</artifactId>
</dependency>
<dependency>
<groupId>dsdf.dsfsdf</groupId>
<artifactId>a</artifactId>
<type>apk</type>
<version>0.0.1-SNAPSHOT</version>
<classifier>${zipaligned-classifier}</classifier> <!-- classifier set according to the ran profile -->
</dependency>
<dependency>
<groupId>dsdf.dsfsdf</groupId>
<artifactId>a</artifactId>
<type>jar</type>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>release</id>
<properties>
<!--
When running a release, we must deployed the final artifact (signed and zipaligned).
-->
<zipaligned-classifier>aligned</zipaligned-classifier>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-jarsigner-plugin</artifactId>
<executions>
<execution>
<id>sign-application-it-apk</id>
<phase>package</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<configuration>
<sign>
<debug>false</debug>
</sign>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
谢谢
I am trying to get Robolectric to work with the Android-release-archetype located https://github.com/akquinet/android-archetypes/wiki/Android-release-archetype
I am having no luck so far
1. I have added the Robolectric dependancy to the POM.
2. I used the goals "clean install"
Then I get erros like these:
[ERROR] /Users/Dewi/Dropbox/mac_projects/a-parent/application-it/src/main/java/dsdf/dsfsdf/a/test/ddd.java:[3,16] package org.junit does not exist
[ERROR] /Users/Dewi/Dropbox/mac_projects/a-parent/application-it/src/main/java/dsdf/dsfsdf/a/test/ddd.java:[4,23] package org.junit.runner does not exist
Can someone please try and get this archetype to work with maven and tell me how to do it?
Here is my test class:
package dsdf.dsfsdf.a.test;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.assertThat;
import org.junit.Test;
import org.junit.runner.RunWith;
import com.xtremelabs.robolectric.RobolectricTestRunner;
@RunWith(RobolectricTestRunner.class)
public class ddd {
@Test
public void asdasd(){
assertThat(5, equalTo(1));
}
}
Here is my parent POM:
<?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>dsdf.dsfsdf</groupId>
<artifactId>a-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>a - Parent</name>
<modules>
<module>application</module>
<module>application-it</module>
</modules>
<dependencies>
<dependency>
<groupId>com.pivotallabs</groupId>
<artifactId>robolectric</artifactId>
<version>1.0-RC1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<type>jar</type>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.1</version>
<type>jar</type>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android-test</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<version>2.8.3</version>
<inherited>true</inherited>
<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>7</platform>
</sdk>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<inherited>true</inherited>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>1.0</version>
</plugin>
<plugin>
<artifactId>maven-release-plugin</artifactId>
<version>2.1</version>
<configuration>
<autoVersionSubmodules>true</autoVersionSubmodules>
</configuration>
</plugin>
<plugin>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.2</version>
<inherited>true</inherited>
<configuration>
<removeExistingSignatures>true</removeExistingSignatures>
<archiveDirectory />
<archive>${project.build.directory}/${project.build.finalName}.${project.packaging}</archive>
<verbose>true</verbose>
<certs>true</certs>
<keystore>${sign.keystore}</keystore>
<alias>${sign.alias}</alias>
<storepass>${sign.storepass}</storepass>
<keypass>${sign.keypass}</keypass>
</configuration>
</plugin>
<plugin>
<groupId>com.pyx4me</groupId>
<artifactId>proguard-maven-plugin</artifactId>
<version>2.0.4</version>
<dependencies>
<dependency>
<groupId>net.sf.proguard</groupId>
<artifactId>proguard</artifactId>
<version>4.4</version>
<scope>runtime</scope>
</dependency>
</dependencies>
<configuration>
<proguardVersion>4.4</proguardVersion>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>
<profiles>
<profile>
<id>release</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<executions>
<execution>
<id>enforce-signing-properties</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<requireProperty>
<property>sign.keystore</property>
<message>The 'sign.keystore' property is missing. It must
contain the path to the keystore used to sign the
application.</message>
</requireProperty>
<requireFilesExist>
<files>
<file>${sign.keystore}</file>
</files>
<message>The 'sign.keystore' property does not point to a
file. It must contain the path to the keystore used to sign
the application.</message>
</requireFilesExist>
<requireProperty>
<property>sign.alias</property>
<message>The 'sign.alias' property is missing. It must
contain the key alias used to sign the application.</message>
</requireProperty>
<requireProperty>
<property>sign.storepass</property>
<message>The 'sign.storepass' property is missing. It must
contain the password of the keystore used to sign the
application.
</message>
</requireProperty>
<requireProperty>
<property>sign.keypass</property>
<message>The 'sign.keypass' property is missing. It must
contain the password of the key used to sign the
application.</message>
</requireProperty>
</rules>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
<profile>
<id>linux</id>
<activation>
<os>
<family>unix</family>
</os>
</activation>
<properties>
<rt.jar.path>${java.home}/jre/lib/rt.jar</rt.jar.path>
<jsse.jar.path>${java.home}/jre/lib/jsse.jar</jsse.jar.path>
</properties>
</profile>
<!-- mac profile has to be after unix since running on mac will trigger
both -->
<profile>
<id>mac</id>
<activation>
<os>
<family>mac</family>
</os>
</activation>
<properties>
<!-- absolute path -->
<!--<rt.jar.path>/System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Classes/classes.jar</rt.jar.path> -->
<!-- or with JAVA_HOME /System/Library/Frameworks/JavaVM.framework/Versions/1.6.0/Home/ -->
<rt.jar.path>${java.home}/../Classes/classes.jar</rt.jar.path>
<jsse.jar.path>${java.home}/../Classes/jsse.jar</jsse.jar.path>
</properties>
</profile>
<profile>
<id>windows</id>
<activation>
<os>
<family>windows</family>
</os>
</activation>
<properties>
<rt.jar.path>${java.home}/jre/lib/rt.jar</rt.jar.path>
<jsse.jar.path>${java.home}/jre/lib/jsse.jar</jsse.jar.path>
</properties>
</profile>
</profiles>
</project>
And here is the child POM:
<?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>
<parent>
<groupId>dsdf.dsfsdf</groupId>
<artifactId>a-parent</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<groupId>dsdf.dsfsdf</groupId>
<artifactId>a-it</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>apk</packaging>
<name>a - Integration tests</name>
<properties>
<!--
When not running a release, we are deploying the plain artifact (so no classifier)
This is overridden during the release.
-->
<zipaligned-classifier></zipaligned-classifier>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android-test</artifactId>
</dependency>
<dependency>
<groupId>dsdf.dsfsdf</groupId>
<artifactId>a</artifactId>
<type>apk</type>
<version>0.0.1-SNAPSHOT</version>
<classifier>${zipaligned-classifier}</classifier> <!-- classifier set according to the ran profile -->
</dependency>
<dependency>
<groupId>dsdf.dsfsdf</groupId>
<artifactId>a</artifactId>
<type>jar</type>
<version>0.0.1-SNAPSHOT</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<extensions>true</extensions>
</plugin>
</plugins>
</build>
<profiles>
<profile>
<id>release</id>
<properties>
<!--
When running a release, we must deployed the final artifact (signed and zipaligned).
-->
<zipaligned-classifier>aligned</zipaligned-classifier>
</properties>
<build>
<plugins>
<plugin>
<artifactId>maven-jarsigner-plugin</artifactId>
<executions>
<execution>
<id>sign-application-it-apk</id>
<phase>package</phase>
<goals>
<goal>sign</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<configuration>
<sign>
<debug>false</debug>
</sign>
</configuration>
</plugin>
</plugins>
</build>
</profile>
</profiles>
</project>
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 pom.xml 示例中添加对 JUnit 的依赖
:
Add dependency to JUnit in pom.xml
example:
我试图用相同的 Maven 结构、相同的原型来测试我的应用程序......一团糟。
显然,测试范围不适用于我的配置,因为当 *-it 模块分离时,执行的主阶段永远不会被视为
test
(如果您检查日志,您会发现它是缺少,此外,模块中的 src/test/java 通常是空的)。我之所以这么假设,是因为当我将 robolectric 和 junit 的范围更改为“提供”时,发生了一些事情,但我没有正确执行测试。仍然缺少一些东西(请参阅我的问题)。< br>
我之所以谈论过去,是因为我放弃了并将 *-it 项目合并到主项目中(我现在将 *-parent 保留在那里)。但这很奇怪,因为我有一个具有完全相同结构的小库,并且它毫不犹豫地运行仪器测试。这样的话,主项目就不会生成任何Activity,只是一个简单的jar库。
I was trying to test my app with the same maven structure, same archetype...a mess.
Apparently the test scope didn't work with my configuration because, when the *-it module is separate, the executed main phase is never considered as
test
(if you check the log you will see that it is missing and, besides, the src/test/java in your modules are usually empty).I am assuming this, because when I changed the scope of both robolectric and junit to "provided", something was happening, but I wasn't having my tests properly executed. Something was still missing (see my question here).
I am talking in the past because I gave up and merged the *-it project into the main one (I'll leave the *-parent there for now). It is very weird though, because I have a little library with the very same structure and it runs Instrumentation test without flinching. In that case, no Activity is generated by the main project, just a simple jar library.