在Jacoco代码覆盖率报告中使用错过的私有方法进行Android单元测试

时间:2017-11-16 06:29:09

标签: android unit-testing mockito code-coverage jacoco

大家好!

我没有测试经验,但我正在尝试为我目前的Android练习项目做这件事。

我一直在研究和阅读关于私有方法的单元测试,人们会说我们不应该测试它们,因为它们是实现而不是行为。

但是,通过使用Jacoco生成代码覆盖率报告,它会声明某些私有方法或成员变量赋值。

我的问题是,我应该重新编写我的方法,还是应该忽略该报告。或者还有其他方法可以测试它们。

谢谢。

屏幕截图和代码:

enter image description here

public class HNStoriesPresenter<V> implements GetHackerNewsContract.StoriesPresenter {    

private static final String TAG = "HNStoriesPresenter";
private GetHackerNewsContract.StoriesView view;
private HackerNewsAPI apiService;
private CompositeDisposable disposables;

private List<HackerNewsStory> hackerNewsStoryList;
private String[] topStoriesID;

// range [0-topStoriesID.length)
private int numStoriesLoaded = 0;

public HNStoriesPresenter(HackerNewsAPI apiService) {
    this.apiService = apiService;
    disposables = new CompositeDisposable();
    hackerNewsStoryList = new ArrayList<>();
}

public void setView(V view){
    this.view = (GetHackerNewsContract.StoriesView) view;
    this.loadTopStories();
}

@Override
public void loadTopStories() {
    // Reset this variable to load stories from start (first page)
    numStoriesLoaded = 0;
    hackerNewsStoryList.clear();

    apiService.getTopStories()
        .subscribeOn(Schedulers.io())
        .observeOn(AndroidSchedulers.mainThread())
        .subscribe(storiesID -> {
                    topStoriesID = storiesID;
                    pullStories();
                },
                error -> view.onFecthStoriesError());
}

@Override
public void loadMoreStories() {
    pullStories();
}

// Function to load NUMBER_OF_STORIES_TO_DISPLAY individual story item
private void pullStories() {
    // Remove previous stories
    hackerNewsStoryList.clear();
    //((GetHackerNewsContract.StoriesView) view).clearStories();

    // Counter to limit total stories to load
    int currentLoaded = 0;
    int start = numStoriesLoaded;
    List<String> storiesToPullList = new ArrayList<>();

    while (numStoriesLoaded < topStoriesID.length && currentLoaded < NUMBER_OF_STORIES_TO_DISPLAY) {
        // Get story item from server
        storiesToPullList.add(topStoriesID[numStoriesLoaded]);
        numStoriesLoaded++;
        currentLoaded++;
    }

    // Use flatMap to ensure the order of items received
    // is same as the list (synchronized).

    // This makes the API call visibly slow. There could
    // be better way but stick with this method for now.
    Single<List<HackerNewsStory>> storiesObservable = Observable.fromIterable(storiesToPullList)
            .flatMap(id -> apiService.getStory(id)).toList();

    storiesObservable
            .subscribeOn(Schedulers.io())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(loadStoriesHandler,
                    error -> view.onFecthStoriesError()
            );

                    //HNStoriesPresenter.this.loadStoriesHandler, HNStoriesPresenter.this.singleErrorHandler);
}

private Consumer<List<HackerNewsStory>> loadStoriesHandler = new Consumer<List<HackerNewsStory>>() {
    @Override
    public void accept(List<HackerNewsStory> hackerNewsStories) throws Exception {
        hackerNewsStoryList.clear();
        hackerNewsStoryList.addAll(hackerNewsStories);

        if (hackerNewsStoryList.size() > 0)
            view.onFetchStoriesSuccess(hackerNewsStoryList);
        else
            view.onFecthStoriesError();

        if (numStoriesLoaded < topStoriesID.length)
            view.showLoadMore();
        else
            view.hideLoadMore();
    }
};
}

0 个答案:

没有答案