Java 使用 JUnit 测试方法 (void)

发布于 2024-10-08 07:32:16 字数 373 浏览 1 评论 0原文

我有以下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

酒几许 2024-10-15 07:32:16

首先,您是否知道可以用 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:

  • Create an object of the class
  • put some entries into friendList
  • call removeFriend() with one of the entries
  • test that this entry is not anymore present
小梨窩很甜 2024-10-15 07:32:16

据推测,friendList 是定义该方法的类上的一个字段。

因此,要进行测试:

  1. 创建该类的一个实例,然后填充friendList,
  2. 并使用应该匹配的字符串调用removeFriend。
  3. 断言friendList不再包含该朋友。

您可能还想通过为不在列表中的朋友传递一个字符串来进行测试,以断言该列表未更改。

编辑:这是您在评论中提出的一个有趣的问题。我通常对这类事情非常明确,所以我会

List<String> friendList = new ArrayList<String>();
friendList.add("friend1");
friendList.add("friend2");

// assert before actually calling the method under test to make sure my setup is ok
assertEquals(2, friendList.size());

// theObj is an instance of the class you are testing
theObj.friendList = friendList; 

// call the method under test
theObj.removeFriend("friend1");

// be explicit, the list should now have size1 and NOT contain the removed friend
assertEquals(1, friendList.size());
assertFalse(friendList.contains("friend1");

注意,我遗漏了一些内容,但这是一个总体轮廓。不要忘记您可以测试删除不在列表中的字符串的情况。您可能还想测试同一个朋友在列表中出现两次的情况。

presumably friendList is a field on the class where the method was defined.

So to test:

  1. Create an instance of the class, and populate friendList
  2. Call removeFriend with a String that should match.
  3. assert that the friendList no longer contains the friend.

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

List<String> friendList = new ArrayList<String>();
friendList.add("friend1");
friendList.add("friend2");

// assert before actually calling the method under test to make sure my setup is ok
assertEquals(2, friendList.size());

// theObj is an instance of the class you are testing
theObj.friendList = friendList; 

// call the method under test
theObj.removeFriend("friend1");

// be explicit, the list should now have size1 and NOT contain the removed friend
assertEquals(1, friendList.size());
assertFalse(friendList.contains("friend1");

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.

究竟谁懂我的在乎 2024-10-15 07:32:16

我假设你的类有不止一种方法。您需要使用这些其他方法来测试调用此方法之前和之后的行为。

顺便说一句:

  • 如果没有找到匹配项,您的方法似乎会删除第一个条目。这是故意的吗?
  • 如果没有条目就会爆炸。
  • 如果friendList中的任何条目为null,则会爆炸,
  • 否则与friendList.remove(rf)做同样的事情;

我会为所有这些条件编写一个测试。

重要的是要寻找你的方法可能失败的所有条件,而不仅仅是它可能起作用的情况。

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

  • appears to remove the first entry if no match is found. Is this intended?
  • would blow up if there were no entries.
  • would blow up if any entries in friendList is null
  • otherwise does the same thing as friendList.remove(rf);

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.

墨洒年华 2024-10-15 07:32:16

无论这个对象是什么,都添加一些朋友,断言他们在那里。调用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.

我偏爱纯白色 2024-10-15 07:32:16

您可能会发现学习“契约编程”风格的类设计很有帮助。它非常适合“测试驱动开发”(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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文