Android系统工具之Roblectric 使用过程中问题总结

      最近在使用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 a JAVA project 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/lib
cp .../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 is not compatible 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 will not run 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.properties file

Set the sdk.dir in a local.properties file by running the following in your project’s root dir:

$ android update project -p .

Setting up a local.properties file 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.properties file

Set the sdk.dir in a local.properties file by running the following in your project’s root dir:

$ android update project -p .

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

ANDROID_HOME environment variable

Set ANDROID_HOME environment variable. You can put this in your .bash_profile for example. You may need to do some extra work to get your IDE to pick it up.

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

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

which android

As a last resort, Robolectric will try running which android to 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_HOME
ls -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








  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值