本文介绍了是否有一个Java单元测试框架可以自动测试getter和setter?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Java(以及其他社区,我确定)中是否存在一个众所周知的争论,即是否应该测试琐碎的getter / setter方法。通常,这与代码覆盖有关。让我们同意这是一个公开辩论,而不是试图在这里回答。



有几篇关于使用Java反射来自动测试这些方法的博客文章。 / p>

是否有任何框架(例如jUnit)提供这样的功能?例如一个注释说这个测试T应该自动测试C类的所有getter / setter,因为我断言它们是标准的。



在我看来,它会增加价值,如果它是可配置的,那么'辩论'将留给用户作为选项。

解决方案

使用静态方法 assertRefEquals


There is a well-known debate in Java (and other communities, I'm sure) whether or not trivial getter/setter methods should be tested. Usually, this is with respect to code coverage. Let's agree that this is an open debate, and not try to answer it here.

There have been several blog posts on using Java reflection to auto-test such methods.

Does any framework (e.g. jUnit) provide such a feature? e.g. An annotation that says "this test T should auto-test all the getters/setters on class C, because I assert that they are standard".

It seems to me that it would add value, and if it were configurable, the 'debate' would be left as an option to the user.

解决方案

Unitils does this w/ the static method assertRefEquals.

这篇关于是否有一个Java单元测试框架可以自动测试getter和setter?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 11:31