Fasaxc
Fasaxc

Reputation: 774

Anyone had success mocking HttpRequests with Robolectric?

I'm just starting with Robolectric. It seems to be working fine to mock most Android classes but when my class under test tries to create a DefaultHttpClient() it gets the dreaded "Stub!" error.

The class under test fails at this line:

HttpClient httpclient = new DefaultHttpClient();

even though the article at http://robolectric.blogspot.com/2011/01/how-to-test-http-requests.html?showComment=1297722651278#c3540420071421225744 seems to suggest this should just work.

My test looks like this:

@Before
public void setUp() throws Exception
{
  Robolectric.addPendingHttpResponse(200, "OK");
  service = new CheckinService();
}

@Test
public void testIt() throws IOException
{
  // Fails at HttpClient httpclient = new DefaultHttpClient()
  service.doStuff(Robolectric.application,
                  REG_ID,
                  TEST_DOMAIN);
}

Any idea what I'm doing wrong?

Upvotes: 4

Views: 1325

Answers (2)

meredrica
meredrica

Reputation: 2563

Fixed it within eclipse. Took me a while to figure out how to fix that but here it is: Go to your test project preferences > build path > order&export > select robolectric jar and press move to top.

Upvotes: 6

Macarse
Macarse

Reputation: 93143

In your pom.xml move the robolectric dependency on top of the android one.

Upvotes: 3

Related Questions