Android 使用Mockito,不调用Observable的map函数
我正在使用Mockito框架测试一个类,该类返回一个可观察的Android 使用Mockito,不调用Observable的map函数,android,mockito,observable,Android,Mockito,Observable,我正在使用Mockito框架测试一个类,该类返回一个可观察的(参见注释): 这是我的实现类: public class DataRepository implements AbstractRepository { private DataSource dataSource; private DataMapper dataMapper; // Constructor public DataRepository(DataSource dataSource, Dat
(参见注释):
这是我的实现类:
public class DataRepository implements AbstractRepository {
private DataSource dataSource;
private DataMapper dataMapper;
// Constructor
public DataRepository(DataSource dataSource, DataMapper dataMapper) {
this.dataSource = dataSource;
this.dataMapper = dataMapper;
}
/**
* The call to dataSource.getItem(int) returns
* an Observable of type ItemResponse.
* So, in the map I cast it to an object of type Item.
**/
public Observable<Item> getItem(int id) {
return dataSource.getItem(id)
.map(new Function<ItemResponse, Item>() {
@Override
public Item apply(ItemResponse itemResponse) throws Exception {
return dataMapper.transform(itemResponse);
}
});
}
}
我收到的错误消息是:
Wanted but not invoked:
dataMapper.transform(
com.my.package.ItemResponse@e720b71
);
-> at com.my.package.test.DataRepositoryTest.testGetItem(DataRepositoryTest.java:28)
Actually, there were zero interactions with this mock.
如何让Mockito调用map()
操作符/方法,然后调用dataSource.getItem(int)
返回的observatable
的apply()
?看起来您没有订阅公共observatable getItem(int id)
返回的observatable
运算符未被调用/执行,请尝试使用dataRepository.getItem(anyInt()).subscribe()代码>仅用于验证
Wanted but not invoked:
dataMapper.transform(
com.my.package.ItemResponse@e720b71
);
-> at com.my.package.test.DataRepositoryTest.testGetItem(DataRepositoryTest.java:28)
Actually, there were zero interactions with this mock.