Maven

To open the project in Eclipse, make sure you have the M2Eclipse plugin installed, then:

  • android update project -p $PATH –target xxx
  • Import… → Existing Maven Projects, find your project.
  • <right click project> → Android → Convert to Android Project
  • <right click “gen” folder> → Build Path → Add to Build Path

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/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 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”

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”

Are these instructions helpful? confusing? Let us know! any feedback is helpful. Thanks -Robolectric

http://pivotal.github.com/robolectric/extending.html


更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. sql plus如何新建新用户
  2. mysql 配置 explicit_defaults_for_times
  3. 怎么用SQL语句实现表中的一个字段加1啊??
  4. mysql表名忽略大小写
  5. ySQL 4.*或5.0.*的升级指南
  6. 如果至少有一条记录的值为1,则返回“是”
  7. 如何将这两个查询组合成1?
  8. 如何在一个查询中组合六个独立查询。表是
  9. mysql主从简单配置
  10. 从SQL字符串分割多桩分隔符