1

I am trying to capture an http request with Robolectric The method tokenize method sends a request to a server and I just want to test that the post is sent: If I try

//Cardtest.java

Robolectric.setDefaultHttpResponse(200, "dummy");
card.tokenize(paymentHandler);
Robolectric.getSentHttpRequest(0);

The I have an empty array error

But I know that the request is sent because if I remove the first line I have the following error:

Unexpected HTTP call POST

If I put log statement it appears that my success block is never called for an http request.

How can I make sure that my HTTP request success call back get called.

(I already try Robolectric.runUiThreadTasksIncludingDelayedTasks();)

Thanks

Elie
  • 131
  • 2
  • 3
  • 12

2 Answers2

1

As we discussed in comments. The issue was because Robolectric was configured to do not intercept http requests:

Robolectric.getFakeHttpLayer().interceptHttpRequests(false)
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114
  • I can't find getFakeHttpLayer() in Robolectric. Can you please tell me in which revision it is added ? – Gem Jul 23 '15 at 09:10
  • 1
    If you use Robolectric 3.0 than you need to include additional dependency http://mvnrepository.com/artifact/org.robolectric/shadows-httpclient/3.0. This only works with apache http client, so if you use UrlConnection than it doesn't work – Eugen Martynov Jul 23 '15 at 16:27
  • Oo i see, but Apache http client is not recommended, It has some issues for android, thats why i am not using it. – Gem Jul 24 '15 at 05:22
  • It is true. Google team recommends to use Urlconnection instead of apache client. As for now most people uses OkHttp. As well most people wrap http to some class that they can mock in unit tests – Eugen Martynov Jul 24 '15 at 09:41
1

Above the 3.0 and newer version You can use

 FakeHttp.getFakeHttpLayer().interceptHttpRequests(false);
Tshunglee
  • 337
  • 2
  • 11