单元测试和断言void方法的案例 [英] Unit testing and assert case for void method

查看:4649
本文介绍了单元测试和断言void方法的案例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为void方法创建一些单元测试。基本上,该方法旨在显示系统用户的角色并在软件中实现它。

I'm trying to create some unit testing for a void method. Basically the method is intended to show the role of a system user and implement it within the software.

这是方法:

public void setPersonObj(Person typeObj)
{
    this.typeObj = typeObj;
    createMain();
}

如何在使用单元测试的单独类中创建断言案例检查这个方法?

How would I create an assert case in a separate class that uses unit testing to check this method?

非常感谢

推荐答案

如果方法是 void 它显然有一些副作用。否则它将是一个无操作。所以你别无选择,需要验证这些副作用。

If the method is void it clearly has some side-effects. Otherwise it would be a no-op. So you have no choice and need to validate these side-effects.

如何测试这些副作用取决于你使用的技术和测试方法:

How to test these side effects is dependant on technology you use and testing approach:


  • 如果该方法调用其他一些协作者/对象,模拟他们并在之后验证模拟

  • if the method calls some other collaborators/objects, mock them and verify mocks afterwards

如果更改某些其他组件的状态(将元素添加到集合,修改字段)查询并断言它们

if it changes the state of some other components (adding element to collection, modifying field) query and assert them

如果

if it stores something on the disk/database, query them as well

在Swing的某个窗口中,您需要使用 Swing 测试框架,例如 Window Licker

if it display some window in Swing, you need to use Swing testing frameworks like Window Licker

如果......您可以提供更多技术详细信息

if... you could provide some more technical details?

BTW执行一些额外无关逻辑的setter是代码气味。维护这些代码的人会发现很难弄清楚这个无辜的 setPersonObj()做了什么,除了......设置。

BTW A setter that performs some extra unrelated logic is a code smell. People maintaining such code will find it really hard to figure out that this innocent setPersonObj() does something except... setting.

还有以下所有名称: PersonObj typeObj 与JavaBean规范的一致性和兼容性应该相同。

Also all these names: Person, PersonObj and typeObj should be the same for consistency and compatibility with JavaBean specification.

这篇关于单元测试和断言void方法的案例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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