最近在使用Robolectric工具对Android 工程进行单元测试,整个过程使用相当纠结。

前提:

Robolectric 工具具体的配置和用法如下:(摘自官网)

Project Creation

Create a project

  • File → New → Project… → Android → Android Project
  • Click “Next”

New Android Project dialog

  • Project Name: MyProject
  • Build Target: Click Target Name: “Google APIs”, Vendor: “Google Inc.”, Platform “2.2”, Api Level: “8”
  • Type ‘com.example’ in package name
  • Check Create Activity, enter “MyActivity”
  • Click “Finish” (Do NOT create an Android Test project)

Add a source test directory to your project

  • Right click on ‘MyProject’ in the package explorer → New… → Folder
  • Folder name: test (do not make this a source folder for this project - hang tight)
  • Click “Finish”

Create aJAVAproject for your tests

Create and configure test Java project

  • File → New → Java Project…
  • Project Name: MyProjectTest
  • Click “Next”
  • Expand the MyProjectTest row and select the “src” row
  • Click link “Remove source folder ‘src’ from build path”
  • Click link “Link additional source”
  • Browse to and select ”…/MyProject/test”
  • Click “Finish” on the “Link additional sources” dialog (keep the new Java project dialog open)

Add dependency on the Android project

  • Select “Projects” tab at the top of the New Java Project dialog
  • Click “Add…”
  • Check “MyProject”
  • Click “OK”
  • Click “Finish” closing the new Java project dialog

Add required directory structure and jars to test project

At the command line:

mkdir -p .../MyProjectTest/libcp .../robolectric-X.X.X-jar-with-dependencies.jar .../MyProjectTest/lib

Configure build path

Back in Eclipse

  • Right click “MyProjectTest”
  • Select “Refresh”
  • Right click “MyProjectTest”
  • Select “Build Path” → “Configure Build Path…”

Add JUnit library

  • Select “Libraries” tab at the top of the Properties dialog for MyProjectTest
  • Click “Add Library”
  • Select “JUnit”
  • Click “Next”
  • Select JUnit library version 4 (Robolectric isnotcompatible with JUnit 3)
  • Click “Finish” (keep the Properties dialog for MyProjectTest open)

Add Robolectric jar

  • Click “Add JARs…”
  • Expand MyProjectTest → lib
  • Select robolectric-X.X.X-jar-with-dependencies.jar
  • Click “OK” (keep the Properties dialog for MyProjectTest open)

Add Android Jars

  • Click “Add External Jars…”
  • Navigate to <your android install directory>/platforms/android-8/android.jar
  • Click “Open” (keep the Properties dialog for MyProjectTest open)
  • Click “Add External Jars…”
  • Navigate to <your android install directory>/add-ons/addon_google_apis_google_inc_8/libs/maps.jar
  • Click “Open”
  • Click “OK” on the Properties for MyProjectTest dialog

Create a test Run Configuration

Your tests willnotrun without this step. Your resources will not be found.

  • “Run” → “Run Configurations…”
  • Double click ”JUnit” (not “Android JUnit Test”)
  • Name: MyProjectTestConfiguration
  • Select the “Run all tests in the selected project, package or source folder:” radio button
  • Click the “Search” button
  • Select “MyProjectTest”
  • TestRunner: JUnit 4
  • Click on the link “Multiple launchers available Select one…” at the bottom of the dialog
  • Check the “Use configuration specific settings” box
  • Select “Eclipse JUnit Launcher”
  • Click “OK”
  • Click the “Arguments” tab
  • Under “Working directory:” select the “Other:” radio button
  • Click “Workspace…”
  • Select “MyProject” (not“MyProjectTest”, The value inside of ‘Other’ edit box should be ‘${workspace_loc:MyProject}’)
  • Click “OK”
  • Click “Close”

Verify your setup

  • Right click the “test” folder under “MyProjectTest”
  • Select “New”→“Class”
  • Package: “com.example”
  • Name: “MyActivityTest”
  • Click “Finish”
  • Add the following source:
package com.example;import com.example.MyActivity;import com.example.R;import com.xtremelabs.robolectric.RobolectricTestRunner;import org.junit.Test;import org.junit.runner.RunWith;import static org.hamcrest.CoreMatchers.equalTo;import static org.junit.Assert.assertThat;@RunWith(RobolectricTestRunner.class)public class MyActivityTest {    @Test    public void shouldHaveHappySmiles() throws Exception {        String hello = new MyActivity().getResources().getString(R.string.hello);        assertThat(hello, equalTo("Hello World, MyActivity!"));    }}

To run the tests

  • “Run” → “Run Configurations…”
  • Select “JUnit” → “MyProjectTestConfiguration”
  • Click “Run“


问题汇总:

在整个过程中出现了以下的问题:

一.在运行单元测试用列MyActivity的时候提示 "../MyProject/./AndroidManifest.xml”

解决措施:

  • Right click on the test
  • “Run As” → “Run Configurations…”
  • Double click “JUnit” (this will magically make the test you’re running appear under JUnit)
  • Select “MyActivityTest” (or the name of whichever test you are currently trying to run)
  • TestRunner: JUnit 4
  • Click on the link “Multiple launchers available Select one…” (or it also may appear as “Using XXX Launcher - Select other…”) at the bottom of the dialog
  • Check the “Use configuration specific settings” box
  • Select “Eclipse JUnit Launcher”
  • Click “OK”
  • Click the “Arguments” tab
  • Under “Working directory:” select the “Other:” radio button
  • Click “Workspace…”
  • Select “MyProject” (not“MyProjectTest”)
  • Click “OK”
  • Click “Close”

二 提示 “no such layout/main”

if you get a RuntimeException saying: “no such layout layout/main”

It means that you have tried to run a test for which you do not have a Run Configuration set up. To remedy this:

  • Right click on the test
  • “Run As” → “Run Configurations…”
  • Double click “JUnit” (this will magically make the test you’re running appear under JUnit)
  • Select “MyActivityTest” (or the name of whichever test you are currently trying to run)
  • TestRunner: JUnit 4
  • Click on the link “Multiple launchers available Select one…” (or it also may appear as “Using XXX Launcher - Select other…”) at the bottom of the dialog
  • Check the “Use configuration specific settings” box
  • Select “Eclipse JUnit Launcher”
  • Click “OK”
  • Click the “Arguments” tab
  • Under “Working directory:” select the “Other:” radio button
  • Click “Workspace…”
  • Select “MyProject” (not“MyProjectTest”)
  • Click “OK”
  • Click “Close”
三 按照上述的方法解决了“AndroidManifest.xml”找不到的问题,但是又提示“Android sdk 的路径找不到”

解决措施:

在你工程的根目录下,加上local.properties , 加上sdk.dir=/home/wangjia/android-sdk ,即可

Robolectric cannot find your Android SDK. You can tell Robolectric how to find your SDK root in several ways:

local.propertiesfile

Set thesdk.dirin alocal.propertiesfile by running the following in your project’s root dir:

$ android update project -p .

Setting up alocal.propertiesfile is a solution that will work for most IDEs since you don’t need to worry about getting environment variables passed around.


四。其他问题汇总(官网上有汇总)

Troubleshooting

java.lang.RuntimeException: Stub!

Make sure that robolectric and its dependencies (including JUnit) appear before the Android API jars in the classpath.

Unable to find Android SDK

Robolectric cannot find your Android SDK. You can tell Robolectric how to find your SDK root in several ways:

local.propertiesfile

Set thesdk.dirin alocal.propertiesfile by running the following in your project’s root dir:

$ android update project -p .

Setting up alocal.propertiesfile is a solution that will work for most IDEs since you don’t need to worry about getting environment variables passed around.

ANDROID_HOMEenvironment variable

SetANDROID_HOMEenvironment variable. You can put this in your.bash_profilefor example. You may need to do someextra workto get your IDE to pick it up.

export ANDROID_HOME=/path/to/android/sdk
android.sdk.pathsystem property

Set the Java system propertyandroid.sdk.path, e.g. by putting-Dandroid.sdk.path=/path/to/android/sdkon the command line.

which android

As a last resort, Robolectric will try runningwhich androidto find the executable on your path. Add the SDK tools to your path:

PATH=/path/to/android/sdk/tools:$PATH

Type com.google.android.maps.MapView not present

java.lang.TypeNotPresentException: Typecom.google.android.maps.MapView not present at org.robolectric.Robolectric.bindShadowClass(Robolectric.java:67)Caused by: java.lang.ClassNotFoundException: caught an exception while obtaining a class file for com.google.android.maps.MapView...
  1. Make sure you have the Google Maps API jar in your build path.
  2. Even if you’re building against an earlier version of the API, link Robolectric to version 7 or higher.

Could not resolve dependencies for project: Could not find artifact com.google.android.maps:maps:jar:16_r2 in central (http://repo1.maven.org/maven2)

The jerk lawyers at Google won’t allow the Google maps add-on library stubs to be uploaded to Maven Central. You need to manually install them yourself.

Make sure you’ve got Android SDK 16 or later downloaded, then do this:

cd $ANDROID_HOMEls -1d add-ons/addon-google_apis-google-* | sort | tail -1 |    xargs -I% mvn install:install-file -DgroupId=com.google.android.maps -DartifactId=maps -Dversion=16_r2 -Dpackaging=jar -Dfile=%/libs/maps.jar








更多相关文章

  1. mybatisplus的坑 insert标签insert into select无参数问题的解决
  2. 2013.03.19(3)———android ActivityGroup的一些问题
  3. 关于有些安卓手机安装应用的时候提示应用未安装 的解决办法之一
  4. android 字符串国际化的问题
  5. Android(安卓)ListView 问题
  6. android orientationchange的问题
  7. 【061】Eclipse 4 Android(安卓)使用技巧
  8. android用户界面之TabHost教程实例汇总
  9. 关于Android资源文件中出现百分号的问题

随机推荐

  1. Android之WebView控件简单使用总结
  2. 49.Android过渡动画的一个小练习
  3. [置顶] android ant 打包全过程 包含libr
  4. [置顶] Android MediaPlayer+Stagefright
  5. Android夜间模式实现(系统自带)
  6. Android中全屏或者取消标题栏
  7. Android官方入门文档[13]暂停和恢复一个A
  8. 监控android binder size
  9. Android常用技术、常用工具和开源项目
  10. Android调试错误-No resource identifier