Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/193.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用Mockito,不调用Observable的map函数_Android_Mockito_Observable - Fatal编程技术网

Android 使用Mockito,不调用Observable的map函数

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

我正在使用Mockito框架测试一个类,该类返回一个可观察的
(参见注释):

这是我的实现类:

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.