Android架构组件:在JUnit测试中未调用Observer

时间:2017-06-26 18:05:01

标签: android unit-testing architecture

看一下这个例子:https://github.com/googlesamples/android-architecture-components/tree/master/GithubBrowserSample 我已经在我的一个项目中实现了相同的模式,但是,我很难让测试按预期工作。

我试图测试我的一个存储库类。该测试检查存储库是否从api获取数据以及观察者的值是否发生变化。

这是测试类

@RunWith(JUnit4.class)
public class TimelineRepositoryTest {

    private SharedPreferences sharedPreferences;
    private DatabaseDao databaseDao;
    private ApiService apiService;
    private TimelineRepository timelineRepository;

    @Rule
    public InstantTaskExecutorRule instantExecutorRule = new InstantTaskExecutorRule();

    @Before
    public void setup() {
        sharedPreferences = mock(SharedPreferences.class);
        databaseDao = mock(DatabaseDao.class);
        apiService = mock(ApiService.class);
        timelineRepository = new TimelineRepository(apiService, sharedPreferences, databaseDao);
        timelineRepository.appExecutors = new InstantAppExecutors();
    }

    @Test
    public void fetchTimelineWithForceFetch() {
        TimelineResponse timelineResponse = new TimelineResponse();
        when(sharedPreferences.getLong(PreferenceUtils.PREFERENCE_LAST_TIMELINE_REFRESH, 0)).thenReturn(0L);
        when(apiService.retrieveTimeline()).thenReturn(ApiUtil.successCall(timelineResponse));

        MutableLiveData<List<Event>> dbData = new MutableLiveData<>();
        when(databaseDao.loadEvents()).thenReturn(dbData);
        Observer observer = mock(Observer.class);

        timelineRepository.getTimelineEvents().observeForever(observer);


        verify(observer).onChanged(Resource.loading(null));
        verify(observer).onChanged(Resource.success(new ArrayList<Event>());
    }
}

此外,这是实际的存储库类:

public class TimelineRepository {

    @Inject AppExecutors appExecutors;
    @Inject @Named("timelineRefreshDurationInMillis") long timelineRefreshDurationInMillis;

    private final DatabaseDao databaseDao;
    private final SharedPreferences sharedPreferences;
    private final ApiService apiService;

    public TimelineRepository(ApiService apiService, SharedPreferences sharedPreferences, DatabaseDao databaseDao) {
        this.apiService = apiService;
        this.sharedPreferences = sharedPreferences;
        this.databaseDao = databaseDao;
    }

    public LiveData<Resource<List<Event>>> getTimelineEvents() {
        return new NetworkBoundResource<List<Event>, TimelineResponse>(appExecutors) {
            @Override
            protected void saveCallResult(@NonNull TimelineResponse timelineResponse) {
                if (timelineResponse.events != null) {
                    databaseDao.saveEvents(timelineResponse.events);
                }
                PreferenceUtils.storeLastTimelineRefreshTimeInMillis(sharedPreferences, System.currentTimeMillis());
            }

            @Override
            protected boolean shouldFetch(@Nullable List<Event> data) {
                return System.currentTimeMillis() - PreferenceUtils.getLastTimelineRefreshTimeInMillis(sharedPreferences) > timelineRefreshDurationInMillis;
            }

            @NonNull
            @Override
            protected LiveData<List<Event>> loadFromDb() {
                return databaseDao.loadEvents();
            }

            @NonNull
            @Override
            protected LiveData<ApiResponse<TimelineResponse>> createCall() {
                return apiService.retrieveTimeline();
            }
        }.getAsLiveData();
    }
}

我想使用测试来检查是否使用不同的值多次调用模拟的观察者。但是,测试表明只使用加载参数调用它。

经过一些调试后,似乎没有调用在构造函数中注册的NetworkBoundResource https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/main/java/com/android/example/github/repository/NetworkBoundResource.java#L48观察者。

有没有人遇到过这个问题?

0 个答案:

没有答案