I am trying to write Unit test with Robolectric and Mockito. I have a retrofit and I would like to test with a mock answer. I tried something like here
but i got it java.lang.NullPointerException. Here is an example of my code:
private MainActivity mainActivity;
@Mock
private FoursquareCalls mockApi;
@Captor
private ArgumentCaptor<Action1<FoursquareListResponse>> cb;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
ActivityController<MainActivity> controller = Robolectric.buildActivity(MainActivity.class);
mainActivity = controller.get();
controller.create();
}
@Test
public void shouldFillAdapterWithReposFromApi() throws Exception {
mockApi.getListFoodTrucks(Mockito.anyString(), Mockito.anyString(), Mockito.anyString(),
Mockito.anyString(), Mockito.anyString(), Mockito.anyString());
Mockito.verify(mockApi).getListFoodTrucks(Mockito.anyString(), Mockito.anyString(), Mockito
.anyString(), Mockito.anyString(), Mockito.anyString(), Mockito.anyString())
.subscribe(cb.capture());
SearchFragment searchFragment = (SearchFragment) mainActivity.getFragmentManager().findFragmentById(R.id
.fragment_container);
FoursquareListResponse testRespo = new FoursquareListResponse();
testRespo.foodtruckReponse = new FoodTruckResponseModel();
testRespo.foodtruckReponse.listFoodtruck = new ArrayList<>();
testRespo.foodtruckReponse.listFoodtruck.add(new FoodTruckResponseModel());
testRespo.foodtruckReponse.listFoodtruck.add(new FoodTruckResponseModel());
cb.getValue().call(testRespo);
assertEquals(searchFragment.getAdapter(), 2);
}
A null pointer exception occurs when I call .subscribe(cb.capture()). I thought I could capture the call inside subsribe()and run my test using a dummy object. I think I didn’t understand something, but I’m not sure which part: /.
source
share