Jacoco代码覆盖率报告中缺少私有方法的Android单元测试
嗨,伙计们 我没有测试方面的经验,但我正在尝试为我目前的Android实践项目进行测试 我一直在研究和阅读关于私有方法的单元测试,人们会说我们不应该测试它们,因为它们是实现而不是行为 但是,通过使用Jacoco生成代码覆盖率报告,它将声明某些私有方法或成员变量分配 我的问题是,我应该重新编写我的方法,还是应该忽略报告。或者有其他方法来测试它们 多谢各位 屏幕截图和代码:Jacoco代码覆盖率报告中缺少私有方法的Android单元测试,android,unit-testing,mockito,code-coverage,jacoco,Android,Unit Testing,Mockito,Code Coverage,Jacoco,嗨,伙计们 我没有测试方面的经验,但我正在尝试为我目前的Android实践项目进行测试 我一直在研究和阅读关于私有方法的单元测试,人们会说我们不应该测试它们,因为它们是实现而不是行为 但是,通过使用Jacoco生成代码覆盖率报告,它将声明某些私有方法或成员变量分配 我的问题是,我应该重新编写我的方法,还是应该忽略报告。或者有其他方法来测试它们 多谢各位 屏幕截图和代码: 公共类HNStoriesPresenter实现GetHackerNewsContract.StoriesPresenter{
公共类HNStoriesPresenter实现GetHackerNewsContract.StoriesPresenter{
私有静态最终字符串TAG=“HNStoriesPresenter”;
私有GetHackerNewsContract.StoriesView视图;
私有HackerNewsAPI服务;
私人合成可处置物品;
私有列表hackerNewsStoryList;
私有字符串[]topstoresid;
//范围[0-上托雷斯内径长度)
private int numstoriesload=0;
公共HNStoriesPresenter(HackerNewsAPI API API服务){
this.apiService=apiService;
可处置的=新的可组合的();
hackerNewsStoryList=新的ArrayList();
}
公共无效设置视图(V视图){
this.view=(GetHackerNewsContract.StoriesView)视图;
这是loadTopStories();
}
@凌驾
公共空荷载上覆层(){
//重置此变量以从开始加载故事(第一页)
numStoriesLoaded=0;
hackerNewsStoryList.clear();
apiService.getTopStories()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.订阅(storiesID->{
topStoriesID=storiesID;
pullStories();
},
错误->查看.onFecthStoriesError());
}
@凌驾
公共新闻{
pullStories();
}
//函数用于加载故事的数量,以显示单个故事项
私家侦探小说(){
//删除以前的故事
hackerNewsStoryList.clear();
//((GetHackerNewsContract.StoriesView)视图);
//计数器以限制要加载的总楼层数
int currentload=0;
int start=numstoriesloadded;
List storiesToPullList=新建ArrayList();
同时(numStoriesLoadedapiService.getStory(id)).toList();
可观察的故事
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.订阅(loadStoriesHandler,
错误->查看.onFecthStoriesError()
);
//HNStoriesPresenter.this.loadStoriesHandler,HNStoriesPresenter.this.singleErrorHandler);
}
私有使用者loadStoriesHandler=新使用者(){
@凌驾
public void accept(List hackerNewsStories)引发异常{
hackerNewsStoryList.clear();
hackerNewsStoryList.addAll(hackerNewsStories);
如果(hackerNewsStoryList.size()>0)
view.onFetchStoriesSuccess(hackerNewsStoryList);
其他的
view.onFecthStoriesError();
if(numStoriesLoaded
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();
}
};
}