Java 使用 JUnit 测试方法 (void)
我有以下 void 方法,但我不知道如何使用 JUnit 测试来测试它。
public void removeFriend(String rf)
{
boolean found = false;
int i = 0;
while(i < friendList.size() && !found)
{
if(friendList.get(i).equals(rf))
{
found = true;
}
else{
i++;
}
}
friendList.remove(i);
}
I have the following void method and i have no idea how to test it with a JUnit test.
public void removeFriend(String rf)
{
boolean found = false;
int i = 0;
while(i < friendList.size() && !found)
{
if(friendList.get(i).equals(rf))
{
found = true;
}
else{
i++;
}
}
friendList.remove(i);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
首先,您是否知道可以用
friendList.remove(rf);
替换整个方法?它将完全按照方法名称声明的方式执行操作(并且保证已经经过彻底测试)。至于测试,即使是“纯粹”单元测试最教条的支持者也不会声称 OO 语言中类的每个方法都必须可以完全隔离地进行测试。否则,您根本无法测试面向对象代码,并且单元测试将仅限于函数式语言。
因此,您在测试中要做的就是创建一个包含该方法的类的对象,使用其构造函数或设置器将其置于该方法可以操作的特定状态,然后调用该方法,然后测试该对象是否处于之后的预期状态。更具体地说:
friendList
中,removeFriend()
,其中一个条目First of all, are you aware that you can replace the entire method with
friendList.remove(rf);
? It will do exactly what the method name claims (and is guaranteed to be already thoroughly tested).As for testing, even the most dogmatic proponents of "pure" unit tests don't claim that each method of a class in an OO language has to be testable in complete isolation. Otherwise, you couldn't test OO code at all and Unit testing would be restricted to functional languages.
So what you do in the test is create an object of the class that contains the method, use its constructor or setters to put it into a specific state the method can operate on, then call the method and then test that the object is in the expected state afterwards. More concretely:
friendList
removeFriend()
with one of the entries据推测,friendList 是定义该方法的类上的一个字段。
因此,要进行测试:
您可能还想通过为不在列表中的朋友传递一个字符串来进行测试,以断言该列表未更改。
编辑:这是您在评论中提出的一个有趣的问题。我通常对这类事情非常明确,所以我会
注意,我遗漏了一些内容,但这是一个总体轮廓。不要忘记您可以测试删除不在列表中的字符串的情况。您可能还想测试同一个朋友在列表中出现两次的情况。
presumably friendList is a field on the class where the method was defined.
So to test:
You probably also want to test by passing in a String for a friend that isn't in the list, to assert that the list is unchanged.
Edit: its an interesting question you raised in the comments. I usually am very explicit with these sorts of things, so I would do
Note that I have left some stuff out, but that is a general outline. Don't forget you can test the case where you remove a String that is NOT in the list. You might also want to test the case where the same friend is in the list two times.
我假设你的类有不止一种方法。您需要使用这些其他方法来测试调用此方法之前和之后的行为。
顺便说一句:
我会为所有这些条件编写一个测试。
重要的是要寻找你的方法可能失败的所有条件,而不仅仅是它可能起作用的情况。
I would assume your class has more than one method. You need to use these other methods to test the behaviour before and after calling this method.
BTW: Your method
I would write a test for all these conditions.
The important thing to look for is all the conditions where your method might break rather than just a situation where it might work.
无论这个对象是什么,都添加一些朋友,断言他们在那里。调用removeFriend(),断言你删除的朋友不存在。
Add some friends to whatever this object is, assert that they're there. Call removeFriend(), assert that the one you removed isn't there.
您可能会发现学习“契约编程”风格的类设计很有帮助。它非常适合“测试驱动开发”(TDD) 风格。简而言之,您仅根据其可访问(公共和受保护)getter 方法返回的值来描述和记录 mutator 方法的用途。
You might find studying the "programming by contract" style of class design helpful. It works well with the "test driven development" (TDD) style. Briefly, you describe and document what a mutator method is meant to do only in terms of values returned by its accessible (public and protected) getter methods.