I have unit test to test user repository and i use custom rule to override the Schedulers and i'm still get the NullPointerException
Here is the test code
public class UserRepositoryTest extends BaseTest {
@Mock
private UserMapper userMapper;
@Mock
private GithubService githubService;
@Mock
private Throwable throwable;
@InjectMocks
private UserRepository userRepository;
@Test
public void fetchUsersEmitsErrorWhenNetworkServiceErrors() {
when(githubService.getUsers()).thenReturn(Single.error(throwable));
userRepository.fetchUsers().test().assertError(throwable);
}
@Test
public void usersRawItemsFromServiceAreMapped() throws Exception {
List<UserRaw> userRawList = createUserRawList();
when(githubService.getUsers()).thenReturn(Single.just(userRawList));
userRepository.fetchUsers().subscribe();
verify(userMapper).apply(userRawList.get(0));
verify(userMapper).apply(userRawList.get(1));
verify(userMapper).apply(userRawList.get(2));
}
private static List<UserRaw> createUserRawList() {
return new ArrayList<UserRaw>() {
{
add(mock(UserRaw.class));
add(mock(UserRaw.class));
add(mock(UserRaw.class));
}
};
}
}
The base class that the test extend from
@RunWith(MockitoJUnitRunner.StrictStubs.class)
public abstract class BaseTest {
@Rule
public final MockitoRule rule = MockitoJUnit.rule();
@Rule
public final RxSchedulerOverrideRule overrideSchedulersRule = new RxSchedulerOverrideRule();
}
The custom rule
public class RxSchedulerOverrideRule implements TestRule {
@Override
public Statement apply(final Statement base, Description description) {
return new Statement() {
@Override
public void evaluate() throws Throwable {
RxJavaPlugins.setIoSchedulerHandler(scheduler -> Schedulers.trampoline());
RxJavaPlugins.setComputationSchedulerHandler(scheduler -> Schedulers.trampoline());
RxJavaPlugins.setNewThreadSchedulerHandler(scheduler -> Schedulers.trampoline());
try {
base.evaluate();
} finally {
RxJavaPlugins.reset();
}
}
};
}
}
and the code from the repository code
public Single<List<User>> fetchUsers() {
return githubService.getUsers()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.flatMapObservable(Observable::fromIterable)
.map(userMapper)
.toList();
}
I think this bug is very weird because i already override the repository Schedulers
This the message
java.lang.NullPointerException at com.ahmedabdelmeged.githubarch.data.UserRepository.fetchUsers(UserRepository.java:41) at com.ahmedabdelmeged.githubarch.data.UserRepositoryTest.usersRawItemsFromServiceAreMapped(UserRepositoryTest.java:46) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.mockito.internal.junit.JUnitRule$1.evaluateSafely(JUnitRule.java:63) at org.mockito.internal.junit.JUnitRule$1.evaluate(JUnitRule.java:43) at com.ahmedabdelmeged.githubarch.common.RxSchedulerOverrideRule$1.evaluate(RxSchedulerOverrideRule.java:24)