简体   繁体   English

单元测试和断言void方法的案例

[英]Unit testing and assert case for void method

I'm trying to create some unit testing for a void method. 我正在尝试为void方法创建一些单元测试。 Basically the method is intended to show the role of a system user and implement it within the software. 基本上,该方法旨在显示系统用户的角色并在软件中实现它。

This is the method: 这是方法:

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? 如何在单独的类中创建一个断言案例,该类使用单元测试来检查此方法?

Many thanks 非常感谢

If the method is void it clearly has some side-effects. 如果该方法void则显然存在一些副作用。 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 如果它在磁盘/数据库上存储了某些东西 ,也可以查询它们

  • if it display some window in Swing, you need to use Swing testing frameworks like Window Licker 如果它在Swing中显示一些窗口,你需要使用像Window Licker这样的Swing测试框架

  • if... you could provide some more technical details ? 如果......您可以提供更多技术细节吗?

BTW A setter that performs some extra unrelated logic is a code smell. BTW执行一些额外无关逻辑的setter是代码气味。 People maintaining such code will find it really hard to figure out that this innocent setPersonObj() does something except... setting. 维护这些代码的人会发现很难弄清楚这个无辜的setPersonObj()除了......设置之外还做了什么。

Also all these names: Person , PersonObj and typeObj should be the same for consistency and compatibility with JavaBean specification. 所有这些名称: PersonPersonObjtypeObj应该与JavaBean规范的一致性和兼容性相同。

There are basically two different philosophies when unit-testing. 单元测试时基本上有两种不同的理念。

  • Testing the state of your class under test. 测试您测试的类的状态 You typically run the method you want to test, and check that the class under test has the correct state after it ran, using getters on it, or the value being returned by the method itself. 您通常运行要测试的方法,并检查被测试的类在运行后是否具有正确的状态,使用其上的getter或方法本身返回的值。 In this scenario, it may be a bit difficult to test a void method, as you may be forced to add getters just for your test, and it would break encapsulation. 在这种情况下,测试void方法可能有点困难,因为您可能不得不为测试添加getter,这会破坏封装。 So don't do that. 所以不要这样做。 An alternate way is to use state-capturing stub classes as your dependencies, but this can be fishy as your stub themselves contain some untested logic. 另一种方法是使用状态捕获存根类作为依赖项,但这可能很糟糕,因为存根本身包含一些未经测试的逻辑。

  • Testing the behaviour of your class under test. 测试您所测试的类的行为 While running the method you want to test, you set expectations on the dependencies of your class. 在运行您要测试的方法时,您可以设置的依赖性期望 This is typically achieved using mock frameworks . 这通常使用模拟框架来实现。 In this scenario, you basically don't care whether your method return a value, or is void. 在这种情况下,您基本上不关心您的方法是返回值还是无效。 What's important is what methods are being called on your dependencies, and with which parameters. 重要的是在依赖项上调用哪些方法,以及使用哪些参数。 This is what you want to do, a far more efficient way of testing (and usually provides better coverage). 这就是你想要做的,一种更有效的测试方法(并且通常提供更好的覆盖)。 Using this way of testing consistently also ensures your design is good and proper OO (forces you to do dependency injection, etc.) 一致地使用这种测试方式也可以确保您的设计良好且适当的OO(强制您进行依赖注入等)

It depends what the createMain() method does. 这取决于createMain()方法的作用。 For example, if the method sets the value of a String field in the class, your unit test should check to make sure that the String field was set to the correct value. 例如,如果方法设置类中String字段的值,则单元测试应检查以确保String字段设置为正确的值。

Your unit test could also check to make sure the typeObj field was set (by calling its corresponding getter method). 您的单元测试还可以检查以确保typeObj字段已设置(通过调用其相应的getter方法)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM