@Test public void getDataTest() { WeatherData getWeatherDataResponse = Mockito.mock(WeatherData.class); when(connector.newGetWeatherDataCall()).thenReturn(getWeatherDataResponse); when(getWeatherDataResponse.call(Matchers.any(Class.class))).thenReturn(new ServiceData()); assertNotNull(service.getData("123")); } Explanation:-
- You got null because you did not set the expected return value. Actually connector.newGetWeatherDataCall() returns null. This is because you did not use Mockito.when() to return your expected results.
- Second : In your case, a method on this return value is invoked so connector.newGetWeatherDataCall() should return a mock of WeatherData. And now you will set an expectation for weatherData.call(..) which will be ServiceData type.