gpt4 book ai didi

使用 Robolectric 和 AndroidAnnotations 进行 Android 测试

转载 作者:太空狗 更新时间:2023-10-29 12:50:26 24 4
gpt4 key购买 nike

我正在使用 AndroidAnnotations 开发应用程序并希望使用 Robolectric 进行单元测试。

虽然我无法让它工作。应用程序和测试放在单个项目中。Source放在/src/main/java下,test放在/src/test/java下。这两个文件夹都是源文件夹。

当使用 Eclipse JUnit 启动器从 Eclipse 运行测试时,我得到:

Class not found org.demoapp.MyActivityTest
java.lang.ClassNotFoundException: org.demoapp.MyActivityTest
at java.net.URLClassLoader$1.run(URLClassLoader.java:202)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.loadClass(RemoteTestRunner.java:693)

当从命令行 mvn clean test 运行时,我得到:

[ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:2.3.2:compile (default-compile) on project demo-app: Compilation failure:
[ERROR] \demo-app\src\test\java\org\demoapp\MyActivityTest.java:[3,16] package org.junit does not exist
[ERROR] \demo-app\src\test\java\org\demoapp\MyActivityTest.java:[4,23] package org.junit.runner does not exist
[ERROR] \demo-app\src\test\java\org\demoapp\MyActivityTest.java:[6,33] package com.xtremelabs.robolectric does not exist
[ERROR] \demo-app\src\test\java\org\demoapp\MyActivityTest.java:[8,1] cannot find symbol
[ERROR] symbol: class RunWith
...
...

尽管所有类都在 eclipse 中编译。

其实我也不知道这里有什么问题。我应该从哪里开始?

<?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>org.demoapp</groupId>
<artifactId>demo-app</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>apk</packaging>
<name>Demo :: App</name>

<properties>
<environment>development</environment>
</properties>

<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>libphonenumber</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.androidannotations</groupId>
<artifactId>androidannotations</artifactId>
</dependency>
<dependency>
<groupId>com.googlecode.androidannotations</groupId>
<artifactId>androidannotations</artifactId>
<classifier>api</classifier>
</dependency>
<dependency>
<groupId>com.pivotallabs</groupId>
<artifactId>robolectric</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
<finalName>${project.artifactId}</finalName>

<sourceDirectory>src</sourceDirectory>

<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.3.0</version>
<extensions>true</extensions>
<configuration>
<manifest>
<debuggable>true</debuggable>
</manifest>
</configuration>
<executions>
<execution>
<id>manifestUpdate</id>
<phase>process-resources</phase>
<goals>
<goal>manifest-update</goal>
</goals>
</execution>
<execution>
<id>alignApk</id>
<phase>package</phase>
<goals>
<goal>zipalign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>

<pluginManagement>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
</plugin>
<plugin>
<artifactId>maven-jarsigner-plugin</artifactId>
<version>1.2</version>
</plugin>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>2.5</version>
<configuration>
<encoding>UTF-8</encoding>
</configuration>
</plugin>

<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>build-helper-maven-plugin</artifactId>
<version>1.5</version>
</plugin>
<plugin>
<groupId>org.eclipse.m2e</groupId>
<artifactId>lifecycle-mapping</artifactId>
<version>1.0.0</version>
<configuration>
<lifecycleMappingMetadata>
<pluginExecutions>
<pluginExecution>
<pluginExecutionFilter>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<versionRange>[3.2.0,)</versionRange>
<goals>
<goal>manifest-update</goal>
</goals>
</pluginExecutionFilter>
<action>
<execute />
</action>
</pluginExecution>
</pluginExecutions>
</lifecycleMappingMetadata>
</configuration>
</plugin>
</plugins>
</pluginManagement>
</build>

<profiles>
<profile>
<id>development</id>
<activation>
<property>
<name>environment</name>
<value>!production</value>
</property>
</activation>
<properties>
<deployment.stage>In Development</deployment.stage>
</properties>
</profile>
<profile>
<id>production</id>
<properties>
<deployment.stage>In Production</deployment.stage>
</properties>
</profile>
<profile>
<id>release</id>
<activation>
<property>
<name>performRelease</name>
<value>true</value>
</property>
</activation>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jarsigner-plugin</artifactId>
<executions>
<execution>
<id>signing</id>
<goals>
<goal>sign</goal>
<goal>verify</goal>
</goals>
<phase>package</phase>
<inherited>true</inherited>
<configuration>
<removeExistingSignatures>true</removeExistingSignatures>
<archiveDirectory />
<includes>
<include>${project.build.directory}/${project.artifactId}.apk</include>
</includes>
<keystore>keystore/keys.keystore</keystore>
<alias>alias</alias>
<storepass>${sign.storepass}</storepass>
<keypass>${sign.keypass}</keypass>
<verbose>true</verbose>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.3.0</version>
<inherited>true</inherited>
<configuration>
<sign>
<debug>false</debug>
</sign>
<zipalign>
<verbose>true</verbose>
<inputApk>${project.build.directory}/${project.artifactId}.apk</inputApk>
<outputApk>${project.build.directory}/${project.artifactId}-signed-aligned.apk
</outputApk>
</zipalign>
<manifest>
<debuggable>false</debuggable>
<versionName>${project.version}</versionName>
<versionCodeAutoIncrement>true</versionCodeAutoIncrement>
</manifest>
<proguard>
<skip>true</skip>
</proguard>
</configuration>
<executions>
<execution>
<id>manifestUpdate</id>
<phase>process-resources</phase>
<goals>
<goal>manifest-update</goal>
</goals>
</execution>
<execution>
<id>alignApk</id>
<phase>package</phase>
<goals>
<goal>zipalign</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</profile>
</profiles>

<dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-io</artifactId>
<version>1.3.2</version>
</dependency>
<dependency>
<groupId>com.googlecode.libphonenumber</groupId>
<artifactId>libphonenumber</artifactId>
<version>5.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>com.googlecode.androidannotations</groupId>
<artifactId>androidannotations</artifactId>
<version>2.6</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.googlecode.androidannotations</groupId>
<artifactId>androidannotations</artifactId>
<classifier>api</classifier>
<version>2.6</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

最佳答案

在您的 pom.xml 中,您将源目录设置为 src , 但你说你的来源在 src/main/java文件夹,您在 src/test/java 中进行测试.

您可以尝试删除 <sourceDirectory>src</sourceDirectory> , 所以 Maven 使用这个默认的文件结构?

此外,就目前而言,我还无法使用 Eclipse 中的 robolectric 启动单元测试...但它适用于 maven。

关于使用 Robolectric 和 AndroidAnnotations 进行 Android 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12628053/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com