如何使用Junit测试工具测试void方法? [英] How to test void method with Junit testing tools?

查看:545
本文介绍了如何使用Junit测试工具测试void方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我碰巧实现了一个方法 void followlink(obj page,obj link),它只是添加页面和链接到队列。我试图测试这种方法是不成功的。



我想要的是测试队列中包含从followlink方法收到的页面和链接。我的测试类已经扩展了TestCase。那么测试这种方法的最佳方法是什么?

解决方案

JUnit FAQ 有一节关于返回 void 的测试方法。在您的情况下,您想要测试名为的方法的副作用。 / p>

FAQ中给出的示例测试了在添加项目后 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());
}


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.

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?

解决方案

The JUnit FAQ has a section on testing methods that return void. In your case, you want to test a side effect of the method called.

The example given in the FAQ tests that the size of a Collection changes after an item is added.

@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());
}

这篇关于如何使用Junit测试工具测试void方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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