Giter VIP home page Giter VIP logo

eclipse-robolectric-example's Introduction

Eclipse-Robolectric-Sample, a pre-configured Robolectric example

 
Blog: Public Static Droid Main

What is this?

This is a pre-configured boilerplate project to help people configure Robolectric in Eclipse and Ant without the use of Maven. Michael Portuesi recently helped me sort this out and I decided to share it to help other people get started.

Setup

After cloning this repository, add the android-project and android-project-test projects into an Eclipse workspace. The android-project is an Android project (obviously), and the android-project-test project is a plain old Java project. You should be able to right-click on the android-project-test project, and choose "Run As -> JUnit Test", and the single test should run via Robolectric and JUnit. Alternatively, you should be able to right-click on the build.xml file and choose "Run As -> Ant Build". This ant script can be used to run your Robolectric tests via a CI server like Jenkins.

Take note of the SampleTestRunner class in com.justinschultz.test.runner, as this is the "secret sauce" (thanks Michael!) that allows Robolectric to find your Android project's manifest and resources. This class inherits from RobolectricTestRunner and should be used to run all of your tests:

package com.justinschultz.tests.runner;

import java.io.File;

import org.junit.runners.model.InitializationError;

import com.xtremelabs.robolectric.RobolectricConfig;
import com.xtremelabs.robolectric.RobolectricTestRunner;

public class SampleTestRunner extends RobolectricTestRunner {

	/**
	 * Call this constructor to specify the location of resources and AndroidManifest.xml.
	 * 
	 * @param testClass
	 * @throws InitializationError
	 */	
	public SampleTestRunner(@SuppressWarnings("rawtypes") Class testClass) throws InitializationError {
		super(testClass, new RobolectricConfig(new File("../android-project/AndroidManifest.xml"), new File("../android-project/res")));
	}
}

Writing Tests

When authoring tests, make sure your class names end with the word "Test", as the ant script requires this. Also make sure that its @RunWith annotation uses the SampleTestRunner.class, and NOT the RobolectricTestRunner.class:

@RunWith(SampleTestRunner.class)
public class SampleTest {
    @Test
    public void testBasicResourceValue() throws Exception {
        String helloFromActivity = new MainActivity().getResources().getString(R.string.hello);
        assertThat(helloFromActivity, equalTo("Hello World, MainActivity!"));
    }
}

Finally, make sure junit.jar is defined in your Ant script’s classpath BEFORE android.jar, otherwise Ant will throw an error:

<path id="junit_classpath">
    <pathelement path="${build.dir}"/>
    <pathelement path="${android.project.classpath}"/>
    <!-- NOTE: junit.jar must come before android.jar! -->
    <filelist refid="libs_jars"/>
    <filelist refid="android_jars"/>
</path>

eclipse-robolectric-example's People

Contributors

murphjm avatar

Watchers

James Cloos avatar Chansuk Yang avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.