How to use Robolectric and still make real HTTP requests

Recently, I have been writing some unit tests for my application using Robolectric. The framework and plugin has allowed me to write some plain unit tests using Mockito which is great, however, today I came across a feature that I didn’t want to take advantage of.

I was testing a class that is responsible for making REST HTTP requests to a server and for this test I wanted to get real responses. However, when running the tests in Android Studio everything was fine, but as soon as I ran it from the command line using gradle I got the following error.

java.lang.RuntimeException: Unexpected call to execute, no pending responses are available. See Robolectric.addPendingResponse(). Request was: GET
	at org.robolectric.shadows.ShadowDefaultRequestDirector.execute(
	at org.apache.http.impl.client.DefaultRequestDirector.execute(
	at org.apache.http.impl.client.AbstractHttpClient.execute(
	at org.apache.http.impl.client.AbstractHttpClient.execute(
	at org.springframework.http.client.HttpComponentsClientHttpRequest.executeInternal(
	at org.springframework.http.client.AbstractBufferingClientHttpRequest.executeInternal(
	at org.springframework.http.client.AbstractClientHttpRequest.execute(
	at org.springframework.web.client.RestTemplate.doExecute(
	at org.springframework.web.client.RestTemplate.execute(
	at org.springframework.web.client.RestTemplate.getForEntity(

This was down to a feature in Robolectric that allows developers to test their code without making real HTTP requests. See their blog article on this here

However, in my situation I wanted to make the real requests. You can turn this feature off using the method call.


I put it in my @Before annotated method so it was configured for all my tests.