为空方法创建Junit测试 [英] Create Junit test for Void method

查看:150
本文介绍了为空方法创建Junit测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习单元测试,我有些困惑,我有一个Void方法,能否请您向我解释如何为该方法创建单元测试.

I am learning unit testing, I have some confusion, I have one Void method, Can you please explain to me how can I create a unit test for this method.

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;
}

}

推荐答案

此方法看起来像它更改了调用它的对象的状态,可以说该对象称为FoodConsumer.

This method looks like it changes the state of object which calls it, lets say the object is called FoodConsumer.

eat(Food)更改两个变量(mAmountOfEatenFoodmIsHungry)的状态.
因此,要知道是否需要再次提供该对象,需要使用mIsHungry的getter方法.最好为mAmountOfEatenFood使用吸气剂.
eat(Food)的明显"结果是对象仍然很饿.

The eat(Food) changes the state of two variables (mAmountOfEatenFood and mIsHungry).
So, to know if you need to feed that object again the getter method for mIsHungry is needed. It would be good to have getter for mAmountOfEatenFood.
The "obvious" result of eat(Food) is if the object is still hungry.

在测试方法中,您需要具有FoodConsumer的实例.因此,在调用eat(Food)之后,您可以像isMHungry()一样调用smth并断言它是true还是false.另外,您可以调用getMAmountOfEatenFood()来检查在进食不足的食物后,是否具有food.eatFood(....)所估计的适当值.

In test method, you need to have an instance of FoodConsumer. Therefore, later after calling eat(Food) you could call smth like isMHungry() and assert if it's true or false. Additionally you could call getMAmountOfEatenFood() to check if after eating insufficient amount of food it has proper value estimated by food.eatFood(....).

为简单起见,您可以将eat(Food)更改为布尔值(即使仅用于测试),并在为mIsHungry设置相同的值后返回true/false.

For a little simplification, you could change the eat(Food) to boolean (even for test only) and return true/false just after setting the same value for mIsHungry.

接下来,我们现在看不到的一些魔术发生在:

Next, some magic, which we can't see now happens in:

food.eatFood(mNeededAmountOfFood - mAmountOfEatenFood)

如果这不是任何一种库方法,并且您可以访问它,那么eatFood返回的值是否合适,最好进行测试收敛.

If this isn't any kind of library method and you have access to it, then it would be good to have tests convering if value returned by eatFood is good.

我无法确切地说出(因为eatFood中的魔法),但是看起来这种方法发现了某种情况,如果FoodConsumer吃得过饱怎么办(我想到的是eatFood,可能超过1).
例如,如果neededFood100并且eatenFood90,则10进入eatFood,该值乘以5.因此eatenFood50递增,导致140amountOfNeededFood不相等.
简单的解决方法是将您的if条件更改为:

I can't say exactly (because of magic in eatFood), but it looks like there is on case uncovered by this method, what if the FoodConsumer over eats (I'm thinking of some conversion rate inside the eatFood which could be over 1).
For example, if neededFood is 100 and eatenFood is 90, then the 10 goes to eatFood which multiplicates that amount by 5. So the eatenFood is incremented with 50 resulting in 140 which is not equal with the amountOfNeededFood.
Simple fix would be to just change your if condition to:

if(mAmountOfEatenFood >= mNeededAmountOfFood){

最后,让我们比较一下这种情况,例如repository.save(User).
类似于repo.save(User)的方法是只具有副作用的void方法.
调用save时没有明确显示这些副作用,但是repo必须/应该包含方法,这些方法可以返回一些值来告知repo的内部状态是否已更改.
在测试存储库时,要检查save方法是否正常工作,测试中的断言是在检查存储库是否返回相同数量的已保存用户,例如,第一个/第二个/第三个用户对象是否包含相同的值save时使用的对象的数量.

In the end, lets compare this case witch something like repository.save(User).
Something similar to repo.save(User) is a void method having only a side effects.
Those side effects aren't being explicitly shown when calling save, but then the repo must/should contain of methods which could return some values telling if the inner state of repo was changed.
When testing the repository, to check if save method works correctly, the assertions in test are checking if repository returns the same number of users which were saved and, for example, if the first/second/third user object contains the same values of the objects used when save.

这篇关于为空方法创建Junit测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆