Skip to content
This repository has been archived by the owner on Jan 14, 2018. It is now read-only.

Testing a RoboSpice request

Stéphane Nicolas edited this page Jul 15, 2015 · 6 revisions

Testing a RoboSpice request is straightforward :

You can use the Android Testing Framework, and write an integration test to verify your request provides the expected result :

import android.test.InstrumentationTestCase;
import android.test.suitebuilder.annotation.LargeTest;

import com.octo.android.robospice.request.simple.SimpleTextRequest;

@LargeTest
public class SimpleTextRequestTest extends InstrumentationTestCase {

	private SimpleTextRequest loremIpsumTextRequest;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		loremIpsumTextRequest = new SimpleTextRequest("http://www.loremipsum.de/downloads/original.txt");
	}

	public void test_loadDataFromNetwork() throws Exception {
		String stringReturned = loremIpsumTextRequest.loadDataFromNetwork();
		assertTrue(stringReturned.startsWith("Lorem ipsum"));
	}
}

In order to test request that use a special service to inject stuff inside requests, like a RestTemplate for the Spring Android Module, you will need to create a SpiceService in order to get injection to work :

@LargeTest
public class SpringAndroidRequestTest extends InstrumentationTestCase {

	private SpringAndroidRequest<Cat> springAndroidRequest;

	@Override
	protected void setUp() throws Exception {
		super.setUp();
		springAndroidRequest = new MyRequest<Cat>(...);
	}

	public void test_loadDataFromNetwork() throws Exception {
		SpringAndroidSpiceService service = new MyService();
		springAndroidRequest.setRestTemplate( service.createRestTemplate() );
		Cat catReturned = springAndroidRequest.loadDataFromNetwork();
		assertTrue(catReturned.getName().equals("Garfield"));
	}
}

As you can see, these tests are synchronous.

Alternative

We also recommand a second approach (used internally in our own testing) : the google mock web server.