我有单元测试来测试用户存储库,我使用自定义规则覆盖调度程序,我仍然得到NullPointerException
这是测试代码
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));
}
};
}
}
测试从
扩展的基类@RunWith(MockitoJUnitRunner.StrictStubs.class)
public abstract class BaseTest {
@Rule
public final MockitoRule rule = MockitoJUnit.rule();
@Rule
public final RxSchedulerOverrideRule overrideSchedulersRule = new RxSchedulerOverrideRule();
}
自定义规则
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();
}
}
};
}
}
以及存储库代码中的代码
public Single<List<User>> fetchUsers() {
return githubService.getUsers()
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.computation())
.flatMapObservable(Observable::fromIterable)
.map(userMapper)
.toList();
}
我认为这个bug非常奇怪,因为我已经覆盖了 存储库调度程序
这条消息
java.lang.NullPointerException at com.ahmedabdelmeged.githubarch.data.UserRepository.fetchUsers(UserRepository.java:41) 在 com.ahmedabdelmeged.githubarch.data.UserRepositoryTest.usersRawItemsFromServiceAreMapped(UserRepositoryTest.java:46) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在java.lang.reflect.Method.invoke(Method.java:498)at org.junit.runners.model.FrameworkMethod $ 1.runReflectiveCall(FrameworkMethod.java:50) 在 org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) 在 org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47) 在 org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) 在 org.mockito.internal.junit.JUnitRule $ 1.evaluateSafely(JUnitRule.java:63) 在org.mockito.internal.junit.JUnitRule $ 1.evaluate(JUnitRule.java:43) 在 com.ahmedabdelmeged.githubarch.common.RxSchedulerOverrideRule $ 1.evaluate(RxSchedulerOverrideRule.java:24)