I'm trying to write a unit test for a piece of code with robolectric. The problem is that I need to fake the http call but seems that robolectric's fake layer it works only with Apache's HttpClient as per this answer:
In Retrofit you can't change the URL so the MockWebServer seems to be not an option.
It seems that mockito can catch the retrofit callbacks but I'm using rxJava so I don't really know if it can helps.
Does anyone has any suggestions about unit testing with Robolectric + Retrofit + okHttp + rxJava?
Here is a small piece of code:
@Test
public void test1() throws IOException, InterruptedException {
FragmentA frag = (FragmentA) activity
.getFragmentManager().findFragmentById(
R.id.frag);
assertThat(frag).isNotNull();
assertThat(frag.isVisible()).isTrue();
EditText input1 = (EditText) frag.getView()
.findViewById(R.id.edit_text1);
EditText input2 = (EditText) frag.getView()
.findViewById(R.id.edit_text2);
Button button = (button) frag.getView()
.findViewById(R.id.button);
input1.setText("999");
input2.setText("999");
Robolectric.addPendingHttpResponse(200, "{\"isValid\": true}");
button.performClick();
assertThat(
ShadowAlertDialog.getLatestDialog() instanceof ProgressDialog)
.isTrue();
}
Robolectric.addPendingHttpResponse won't works anyway because of the OkHttp. The api call is started when the button is pressed so in that I moment I need to fake the response!