gpt4 book ai didi

android - 使用 Mockito 进行 JVM 单元测试以测试网络请求的 Retrofit2 和 RxJava

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:07:39 26 4
gpt4 key购买 nike

Android Studio 2.3 RC 1

我正在使用 MVP 架构并希望运行 JVM 单元测试。

在我的模型中,我使用 Retrofit2 和 RxJava 从 API 获取电影。我想测试函数 getPopularMovies(...) 但是,此函数将调用网络服务器。然而,在测试中我想以某种方式模拟它并测试 onSuccess()onFailure() 方法被调用。

我的模型类看起来像这个 fragment 只是为了保持简短:

public class MovieListModelImp implements MovieListModelContract {

@Override
public void getPopularMovies(PopularMovieResultsListener popularMovieResultsListener) {
mSubscription = mMovieAPIService.getPopular(Constants.MOVIES_API_KEY)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(new Subscriber<Results>() {
@Override
public void onCompleted() {
Timber.d("onCompleted");
}

@Override
public void onError(Throwable e) {
Timber.e(e, "onError");
popularMovieResultsListener.onFailure(e.getMessage());
}

@Override
public void onNext(Results results) {
Timber.d("onNext %d", results.getResults().size());
popularMovieResultsListener.onSuccess(results);
}
});
}
}

界面:

public interface MovieListModelContract {
interface PopularMovieResultsListener {
void onFailure(String errorMessage);
void onSuccess(Results popularMovies);
}
void getPopularMovies(PopularMovieResultsListener popularMovieResultsListener);
}

我要解决的问题是如何在不实际调用网络服务的情况下使用 Mockito 测试 getPopularMovies?我只想测试一下:popularMoviesResultsListener.onFailure(e.getMessage()) 获取电影失败时调用和popularMovieResultsListener.onSuccess(results); 接收到电影成功时调用

我有这样的测试,但我不确定这是否正确:

@Test
public void shouldDisplaySuccessWhenNetworkSucceeds() {
/* Results is the movie results class that is returned */
Results results = new Results();

/* Mock the listener */
MovieListModelContract.PopularMovieResultsListener mockPopularMoviesResultsListener =
Mockito.mock(MovieListModelContract.PopularMovieResultsListener.class);

/* Real instance of the model */
MovieListModelImp movieListModelImp = new MovieListModelImp();

/* Call getPopularMovies with mock listener - However, this will still make a real network request */
movieListModelImp.getPopularMovies(mockPopularMoviesResultsListener);

/* Verify - but I think I have got this all wrong */
verify(mockPopularMoviesResultsListener, times(1)).onSuccess(results);
}

所以我的问题是如何模拟对网络请求的调用并测试预期的 onSuccess() 和 onFailure() 是否正常工作?

最佳答案

这个想法是让用户 TestSubscriber 在单元测试中断言。

Return the Observable from Retrofit instead of void

(请注意,我已经删除了监听器 PopularMovieResultsListener,因为您正在使用 RxJava。使用 RxJava,您可以订阅返回的 Observable 并使用 onNext() onComplete()onError() 代替。)

public class MovieListModelImp implements MovieListModelContract {

@Override
public Observable<Results> getPopularMovies() {
/** Return the Observable from Retrofit. */
return mMovieAPIService.getPopular(Constants.MOVIES_API_KEY);
}

Use TestSubscriber in your unit tests to assert

@Mock
MovieAPIService mMovieAPIService

@Test
public void shouldDisplaySuccessWhenNetworkSucceeds() {

/* Results is the movie results class that is returned */
Results expectedResults = new Results();


MovieListModelImp movieListModelImp = new MovieListModelImp();
//Mock mMovieAPIService which is the actual network call
when(mMovieAPIService.getPopular(any(String.class)).thenReturn(Observable.just(results));

Observable<Results> actualResultsObservable = movieListModelImp.getPopularMovies();
TestObserver<Results> testObserver = actualResultsObservable.test();
testObserver.assertSubscribed();

testObserver.assertResult(expectedResults);

//verify
verify(mMovieAPIService, times(1)).getPopular(any(String.class));

}

关于android - 使用 Mockito 进行 JVM 单元测试以测试网络请求的 Retrofit2 和 RxJava,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42445665/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com