【发布时间】:2020-01-28 02:45:35
【问题描述】:
我正在学习单元测试,我有些困惑,我有一个 Void 方法,请你解释一下如何为这个方法创建一个单元测试。
public void eat(Food food){
mAmountOfEatenFood += food.eatFood(mNeededAmountOfFood - mAmountOfEatenFood);
if(mAmountOfEatenFood == mNeededAmountOfFood){
System.out.println(mName + " has eaten " + mAmountOfEatenFood + " and will not be hungry for a while..");
mAmountOfEatenFood = 0;
mIsHungry = false;
}else{
System.out.println(mName + " has eaten " + mAmountOfEatenFood + " and is still hungry..");
System.out.println("Needs= " + mNeededAmountOfFood);
mIsHungry = true;
}
}
【问题讨论】:
-
你想在 void 方法中测试什么?
-
@Deadpool 实际上我有一个新项目,我需要在其中创建一个单元测试,我开始研究它并完成了一些 Util 类,但是当我来到一个 Void 方法时,有一些逻辑,那时我不知道如何为此创建测试,实际上我需要测试什么。希望你明白我的意思。
-
对于 void 方法,您只需使用
Assert.assertEquals() -
@VishwaRatna 那么我如何在该方法中覆盖 5-6 行逻辑?
标签: java android unit-testing mockito junit4