Android 如何检查mockito是否具有mock函数,并按照when.ThenReturn中的说明返回
莫基托-什么时候→thenReturn未按预期工作。这是在同一个UnitTest类中工作。我有其他的功能,我测试了相同的方式是工作的预期 以下是我的测试方法Android 如何检查mockito是否具有mock函数,并按照when.ThenReturn中的说明返回,android,junit,mockito,Android,Junit,Mockito,莫基托-什么时候→thenReturn未按预期工作。这是在同一个UnitTest类中工作。我有其他的功能,我测试了相同的方式是工作的预期 以下是我的测试方法 @Test public void testAlerts_whenReturnsData() { //Assemble Product product = new Product(null, null, 0, "-1"); when(api.alerts("-1", null, null)) .thenReturn(Single.
@Test
public void testAlerts_whenReturnsData() {
//Assemble
Product product = new Product(null, null, 0, "-1");
when(api.alerts("-1", null, null))
.thenReturn(Single.just(product));
//Act
viewModel.getAlertsData().observeForever(dataObserver);
viewModel.getIsPhoneLoadingData().observeForever(loadingObserver);
viewModel.getAlerts("1234567890");
//Verify
verify(loadingObserver).onChanged(true);
verify(dataObserver).onChanged(product);
verify(loadingObserver).onChanged(false);
}
我在类中的实际getAlerts()方法:
void getAlerts(String phone) {
isPhoneLoadingData.setValue(true);
alertDisposable =
api.alerts(id,
userManager.getNonNullUserId(), AlertsRequest.create(phone, true))
.doOnEvent((product, throwable) -> isPhoneLoadingData.postValue(false))
.compose(RxSingleSchedulers.DEFAULT.applySchedulers())
.subscribe(alertsData::setValue, errorData::setValue);
}
异常:似乎无法模拟api.alerts(字符串、字符串、AlertRequest)
试用
when(api.alerts(any(String.class), any(String.class), any(AlertsRequest.class)))
.thenReturn(Single.just(product));
//Api->警报
public interface Api {
Single<Product> alerts(
String Id,
String customerId,
AlertsRequest request
);
}
公共接口Api{
单一警报(
字符串Id,
字符串customerId,
警报请求
);
}
getAlerts中.doOnEvent-api.alerts()处的java.lang.NullPointerException
如何检查mockito是否能够模拟该函数,并将按照when.中的定义返回。然后返回
我能想到这些可能的原因:
您是否仔细检查了导致NullPointerException
的所有其他可能原因?
(未初始化的api
,userManager
,isPhoneLoadingData
等)
null
参数与any(String.class)
不匹配
如果有多个alert
方法具有相同的参数计数,传递null
会使其不明确。在这种情况下,您需要强制转换null参数,例如(String)null
使用JUnit5,Mockito可以检测
与模拟配置不匹配的方法调用
不受任何调用影响的模拟配置
当您使用org.mockito.junit.jupiter.MockitoExtension
啊<代码>警报请求。创建(电话,真)
是罪魁祸首。被困在同样的场景中;头脑清醒&改天,你可以从不同的角度看待你的问题
我在嘲笑
when(api.alerts("-1", null, null))
.thenReturn(Single.just(product));
在哪里
考试通过了<代码>api.alerts(“-1”,null,null)
->为什么在这里传递null?你不能传递一些类似的东西吗?在api.alerts()中做了什么?@MDikkii:我尝试过使用api.alerts(“-1”,null,null)→ 警报(any(String.class)、any(String.class)、any(AlertsRequest.class)但是它仍然抛出NPE。看起来Mockito不能模拟这个方法。你能显示API.Alcess方法吗?考虑张贴A并包含完整的StActTrac迹。你也可能想添加更多关于“代码> API <代码> >类及其<代码>警报< /代码>方法的更多信息。我完全错过了上下文。是的,我检查了是否有任何变量未初始化或其他。但是那边一切都好。因为我在同一个类中有相同类型的方法,它工作得非常好。唯一的区别是警报有3个参数,这是自定义对象。
// here AlertsRequest.create(phone, true) mocking as null.
// But when actual call is made it creates a Object of AlertsRequest with phone as null & another parameter as true
// that's the reason mocking was creating for api.alerts("-1", null, null)
// here is it was api.alerts("-1", null, AlertsRequest(null, true))
api.alerts(id, userManager.getNonNullUserId(), AlertsRequest.create(phone, true))
void getAlerts(AlertsRequest request) {
isPhoneLoadingData.setValue(true);
alertDisposable =
api.alerts(id,
userManager.getNonNullUserId(), request)
.doOnEvent((product, throwable) -> isPhoneLoadingData.postValue(false))
.compose(RxSingleSchedulers.DEFAULT.applySchedulers())
.subscribe(alertsData::setValue, errorData::setValue);
}