受到 Just Say No to More End-to-End Tests
文章链接:http://googletesting.blogspot.tw/2015/04/just-say-no-to-more-end-to-end-tests.html
该文章启迪,决定对单元测试的内容稍做整理
Reboletric被测工程是Android Project,而测试工程是Java Project,执行的测试是Junit Test,并非Android Junit Test,跳过了模拟器,实现高效率运行
Reboletric官网:http://robolectric.org/getting-started/
工程环境Eclipse+maven(带M2Eclipse)插件
工程下载地址:
https://github.com/priyaaank/RobolectricSample
环境中,maven版本为3.1.1(需要根据情况更新到新版本);
robolectric包版本为:robolectric-2.4-jar-with-dependencies.jar
下载地址: https://repo1.maven.org/maven2/org/robolectric/robolectric/2.4/
M2Eclipse推荐安装方式:
插件方式,Help-Install new Software- 输入以下地址,
http://download.eclipse.org/technology/m2e/releases/
RobolectricSample示例工程
1.下载示例工程
2.命令行下切换到工程目录,执行android update project -p . --target 1
(--target指定android的api level,android list命令可查询)
3.import 导入示例工程
4.配置build path,添加robolectric-***-jar-with-dependencies.jar包;
添加<your android install directory>/add-ons/addon-google_apis-google-**/libs/maps.jar
关于Google Maps API maps.jar file for android
First of all update your SDK->Google Play Services and then
Find this Path in your Computer :
...\android-sdk-windows\add-ons\addon-google_apis-google-17\libs\maps.jar
add this jar using Add External Jar
...
导入包后的build path路径下如图所示:
5.右键test下工程,Right-click -> Run As ->JUnit Test
会提示如下对话框:
点击OK取消后,会弹出一个配置的对话框
6.
下载Android.jar
地址:http://www.java2s.com/Code/Jar/a/Downloadandroidjar.htm
配置Run Configuration,Right-click -> Run As -> Run Configurations
Tab: Classpath -> User Entries -> Add External JARs ...
找到android.jar
7.运行测试用例;右键点击测试用例,选择Run As -> Junit Test
执行后查看结果:
HttpTest.java这个case中导入的包
import com.google.inject.internal.Maps;
查到存在此文件的JAR包:
com.google.inject.internal.Maps.java
http://www.java2s.com/Code/Jar/g/Downloadguicepatches214sourcesjar.htm
执行mvn clean test后提示找不到com.google.inject.internal
笔者改为:
import com.google.inject.internal.util.$Maps;
com.google.inject.internal.util
com.google.inject.internal.util.Maps
http://bornjohannes-codex.eclipselabs.org.codespot.com/git/Imports/index.html?com/google/inject/internal/util/package-tree.html
执行后成功
可能遇到的问题:
1.Project build error: Unknown packaging: apk
在Eclipse中安装m2e-android插件
安装源:http://download.eclipse.org/technology/m2e/releases/
安装方法:Help -> Install new Software -> 在出来的对话框中点击 Add ->
Name:m2e-android
若m2e装好后仍未解决,检查Maven配置
Windows—>Preferences—>Maven—>User Settings,查看setting.xml
确保这里的Maven不是用的Eclipse自带的,而是本地下载的新版本的Maven
2.mvn clean install 报错:
Failure to find org.springframework:spring:jar:3.0.0.RELEASE in http://download.java.net/maven/2/ was cached in the local repository, resolution will not be reattempted until the update interval of Java.Net has elapsed or updates are forced -> [Help 1] 解决办法: Not sure if this works for Maven 3, here's what would work for Maven 2. This should work to clean the local repository: mvn dependency:purge-local-repository Otherwise, try cleaning the specific artifact from your local repository: * Linux: ~/.m2 * Windows: C:\Documents and Settings\USER\.m2
另外见上两篇博文maven的两个坑:
3.http://www.cnblogs.com/melody-emma/p/5112658.html
4.http://www.cnblogs.com/melody-emma/p/5112722.html
5.@Override 的代码全部都报错
默认Maven中的JAVA版本是1.5,只要修改成1.6就好了
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.1</version>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
希望本文能帮助你不再被坑
参考文章:http://blog.csdn.net/hunterno4/article/details/20653817