2011-03-08 134 views
8

我對使用Maven構建的Android項目有疑問, 我們讓它運行我們的活動測試,但現在我們需要它來運行單元測試。 單元測試與活動測試在同一個項目中,我如何在我們的pom.xml文件中進行設置?Android與Maven單元測試

這是父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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 
    <groupId>andersen.project</groupId> 
    <artifactId>Hello-parent</artifactId> 
    <version>1.0</version> 
    <packaging>pom</packaging> 
    <name>Hello - Parent</name> 

    <modules> 
    <module>HelloWorld</module> 
    <module>HelloWorldTest</module> 
    </modules> 

    <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> 
    <sourceDirectory>src</sourceDirectory> 
    <testSourceDirectory>test</testSourceDirectory> 
    <pluginManagement> 
     <plugins> 
     <plugin> 
      <groupId>com.jayway.maven.plugins.android.generation2</groupId> 
      <artifactId>maven-android-plugin</artifactId> 
      <version>2.8.3</version> 
     </plugin> 
     <plugin> 
      <artifactId>maven-compiler-plugin</artifactId> 
      <version>2.3.2</version> 
     </plugin> 
     </plugins> 
    </pluginManagement> 
    </build> 
</project> 

這是應用程序的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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <parent> 
    <groupId>andersen.project</groupId> 
    <artifactId>Hello-parent</artifactId> 
    <version>1.0</version> 
    </parent> 

    <groupId>andersen.project</groupId> 
    <artifactId>helloworld</artifactId> 
    <version>1.0</version> 
    <packaging>apk</packaging> 
    <name>HelloWorld - Application</name> 

    <dependencies> 
    <dependency> 
     <groupId>com.google.android</groupId> 
     <artifactId>android</artifactId> 
    </dependency> 
    </dependencies> 
    <build> 
    <plugins> 
     <plugin> 
     <groupId>com.jayway.maven.plugins.android.generation2</groupId> 
     <artifactId>maven-android-plugin</artifactId> 
     <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>4</platform> 
      </sdk> 
      <deleteConflictingFiles>true</deleteConflictingFiles> 
      <undeployBeforeDeploy>true</undeployBeforeDeploy> 
     </configuration> 
     <extensions>true</extensions> 
     </plugin> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <configuration> 
      <source>1.5</source> 
      <target>1.5</target> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

,這是測試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/maven-v4_0_0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <parent> 
    <groupId>andersen.project</groupId> 
    <artifactId>Hello-parent</artifactId> 
    <version>1.0</version> 
    </parent> 

    <groupId>andersen.project</groupId> 
    <artifactId>helloworldtest</artifactId> 
    <version>1.0</version> 
    <packaging>apk</packaging> 
    <name>HalloWorld - Integration tests</name> 

    <dependencies> 
    <dependency> 
     <groupId>com.google.android</groupId> 
     <artifactId>android</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>com.google.android</groupId> 
     <artifactId>android-test</artifactId> 
    </dependency> 
    <dependency> 
     <groupId>andersen.project</groupId> 
     <artifactId>helloworld</artifactId> 
     <type>apk</type> 
     <version>1.0</version> 
    </dependency> 
    <dependency> 
     <groupId>andersen.project</groupId> 
     <artifactId>helloworld</artifactId> 
     <type>jar</type> 
     <version>1.0</version> 
    </dependency> 
    </dependencies> 
    <build> 
    <plugins> 
     <plugin> 
     <groupId>com.jayway.maven.plugins.android.generation2</groupId> 
     <artifactId>maven-android-plugin</artifactId> 
     <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>4</platform> 
      </sdk> 
      <deleteConflictingFiles>true</deleteConflictingFiles> 
      <undeployBeforeDeploy>true</undeployBeforeDeploy> 
     </configuration> 
     <extensions>true</extensions> 
     </plugin> 
     <plugin> 
     <artifactId>maven-compiler-plugin</artifactId> 
     <configuration> 
      <source>1.5</source> 
      <target>1.5</target> 
     </configuration> 
     </plugin> 
    </plugins> 
    </build> 
</project> 

我應該在父pom.xml中添加以下文本嗎?:

<dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>4.8.1</version> 
    <scope>test</scope> 
</dependency> 

並將它添加到其他poms?

當我運行Maven的輸出是:

Junit.framework不存在

+0

當然,如果你正在使用JUnit必須添加的依賴。 – khmarbaise 2011-03-08 12:14:20

+0

是的,但仍然失敗。 Junit.framework不存在。 – Casper 2011-03-08 12:20:22

回答

0

添加Surefire plugin到測試的pom.xml。 Surefire運行單元測試。

+0

我已經添加了對父pom.xml文件和測試pom.xml文件的依賴關係。但它仍然失敗:junit.framework不存在 – Casper 2011-03-08 12:54:47

+0

@Casper:不要忘記將單元測試添加到模塊中:http://maven.apache.org/plugins/maven-surefire-plugin/examples/junit.html – 2011-03-08 12:58:53

+0

請發佈整個錯誤消息。誰說的?神火? Java編譯器? – 2011-03-08 12:59:45

7

我面臨同樣的問題。這是由父母的錯誤配置造成的。您必須在dependencyManagement部分中刪除範圍標記。

像這樣:

<dependencyManagement> 
    <dependencies> 
    <dependency> 
     <groupId>com.google.android</groupId> 
     <artifactId>android</artifactId> 
     <version>2.2.1</version> 
    </dependency> 
    <dependency> 
     <groupId>com.google.android</groupId> 
     <artifactId>android-test</artifactId> 
     <version>2.2.1</version> 
    </dependency> 
    </dependencies> 
</dependencyManagement> 
+1

maven也存在一個TEST範圍! http://maven.apache.org/guides/introduction/introduction-to-dependency-mechanism.html#Dependency_Scope。 – 2013-11-03 16:33:07