Java 如何使用 Junit 测试工具测试 void 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1244541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to test void method with Junit testing tools?
提问by user152462
I just happen to implement a method void followlink(obj page,obj link)
which simply adds page and link to queue. I have unsuccessfully tried to test this kind of method.
我只是碰巧实现了一个方法void followlink(obj page,obj link)
,它只是将页面和链接添加到队列中。我曾尝试测试这种方法没有成功。
All I want is to test that in the queue contains page and link received from followlink method. My test class already extends TestCase. So what is the best way to test such a method?
我想要的只是测试队列中是否包含从 followlink 方法收到的页面和链接。我的测试类已经扩展了 TestCase。那么测试这种方法的最佳方法是什么?
回答by Patrick McDonald
You could test the size if the queue before and after calling your method, something like:
如果队列在调用您的方法之前和之后,您可以测试它的大小,例如:
int size = queue.length();
followLink(page, link);
assertEquals(size+1, queue.length()); // or maybe size+2?
another thing you might do is start off with an empty queue, call followLink, then dequeue the first item and test its values.
您可能要做的另一件事是从一个空队列开始,调用 followLink,然后将第一项出列并测试其值。
回答by Mnementh
So - check after the call of the method the queue, if the values passed to the method are added to the queue. You need access to the queue (something like getQueue()) for this.
因此 - 在调用队列方法后检查传递给方法的值是否已添加到队列中。为此,您需要访问队列(类似于 getQueue())。
回答by Bill the Lizard
The JUnit FAQhas a section on testing methods that return void
. In your case, you want to test a side effectof the method called.
该JUnit FAQ中对测试方法返回一个部分void
。在您的情况下,您想测试所调用方法的副作用。
The example given in the FAQ tests that the size of a Collection
changes after an item is added.
常见问题解答中给出的示例测试Collection
添加项目后更改的大小。
@Test
public void testCollectionAdd() {
Collection collection = new ArrayList();
assertEquals(0, collection.size());
collection.add("itemA");
assertEquals(1, collection.size());
collection.add("itemB");
assertEquals(2, collection.size());
}
回答by Chris Ciesielski
Use jMock library and mock a storagethat holds your queue, jMock allows to assert whether mocked method was called and with what parameters.
使用 jMock 库并模拟保存您的队列的存储,jMock 允许断言是否调用了模拟方法以及使用了哪些参数。
回答by CPerkins
Most likely, your queue is private, so checking the size isn't going to work. The "design for testing" solution I've seen is to make use of package privatemethods and members instead. Since your junit tests are likely to be in the same package, they'll have access.
很可能,您的队列是private,因此检查大小不起作用。我见过的“测试设计”解决方案是使用包私有方法和成员。由于您的 junit 测试可能位于同一个包中,因此它们将具有访问权限。
That by itself lets the "queue.length()" side effect test work.
这本身让“ queue.length()”副作用测试工作。
But I'd go further: really, you should consider checking that your method has inserted the correctpage and link to your queue. The details for that require more knowledge about how you're representing (and combining) page and link.
但我会更进一步:实际上,您应该考虑检查您的方法是否插入了正确的页面并链接到您的队列。详细信息需要更多关于您如何表示(和组合)页面和链接的知识。
The jMocksolution is also very good, though in truth I'm much more familiar with writing my own test harnesses.
该JMock的解决方案也很不错,虽然事实上我更熟悉,写我自己的测试工具。
回答by subodhkarwa
We have two ways to check the following :
我们有两种方法可以检查以下内容:
Verify using the size of the Queue/Collection as in the program below : (As pointed out by Patrick)
@Test public void testfollowlinkAdd() { int size = queue.length(); queue.add(pageLink); //Considering an Object of PageLink Assert.assertTrue(size+1, queue.length()); }
使用队列/集合的大小进行验证,如下面的程序所示:(正如帕特里克所指出的)
@Test public void testfollowlinkAdd() { int size = queue.length(); queue.add(pageLink); //Considering an Object of PageLink Assert.assertTrue(size+1, queue.length()); }
OR
或者
- Make use of MockObjects. Frameworks like Mockito can be helpful here.
- 使用 MockObjects。像 Mockito 这样的框架在这里很有帮助。