4

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?

Fasaxc
  • 756
  • 4
  • 17

2 Answers2

6

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.

meredrica
  • 2,563
  • 1
  • 21
  • 24
  • That sounds like what I needed. – Fasaxc Oct 04 '11 at 20:36
  • This method solves tons of seemingly random problems in Eclipse + ADT. I didn't think to use it here! – mjama Aug 03 '12 at 16:20
  • Be aware that if you are using a newer version of the android-support-library than robolectric, you'll need to make sure that goes above robolectric in the build path. – Gabriel Nov 21 '13 at 23:40
  • With IntelliJ/Android Studio and Maven, I had to move the Android platform beneath _all_ of the Maven dependencies, not just Robolectric. – Sam Mar 13 '14 at 21:25
3

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

Macarse
  • 91,829
  • 44
  • 175
  • 230