Java getter 和 setter 的 junit 测试方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2218177/
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
junit test method for getters & setters
提问by Girish Bhatt
I have many java beans in my project. I need to generate a JUnit Test class for them. The test methods generated using Eclipse 3.2 & junit 4.4 look like the following:
我的项目中有很多 java bean。我需要为他们生成一个 JUnit 测试类。使用 Eclipse 3.2 & junit 4.4 生成的测试方法如下所示:
public void testGetName() {
// fail("Not yet implemented");
}
@Test
public void testSetName() {
// fail("Not yet implemented");
}
@Test
public void testGetEmployeeid() {
// fail("Not yet implemented");
}
@Test
public void testSetEmployeeid() {
// fail("Not yet implemented");
}
some of my beans have more than 100 fields...
我的一些豆子有 100 多个字段...
Is there a way by which I can get a single test method for both the getters & setters like testEmployeeid()
, testName()
so that in these methods I can test both my setters & getters rather than using 2 diff. test methods for them...
有没有一种方法可以为 getter 和 setter 获得一个单一的测试方法,例如 testEmployeeid()
,testName()
以便在这些方法中我可以同时测试我的 setter 和 getter,而不是使用 2 diff。他们的测试方法...
How should I configure eclipse to do this?
我应该如何配置 eclipse 来做到这一点?
回答by Péter T?r?k
The philosophy of Test Driven Development says "test everything which can possibly break". That is, focus your efforts on the useful tests, instead of writing tests for just the sake of it.
测试驱动开发的理念是“测试所有可能破坏的东西”。也就是说,把你的精力集中在有用的测试上,而不是仅仅为了它而编写测试。
Getters and setters are almost always trivial code, which is not worth testing by themselves.
getter 和 setter 几乎都是微不足道的代码,不值得自己测试。
I know this is not a straight answer to your plea, but I thought it may still help to point this out ;-) So why do you actually need to write tests for all those getters and setters in the first place?
我知道这不是对您请求的直接回答,但我认为指出这一点可能仍然有帮助;-) 那么,为什么您实际上首先需要为所有这些 getter 和 setter 编写测试?
回答by monojohnny
You could perhaps use Apache Commons 'beanutils' to help automate this:
您也许可以使用 Apache Commons 'beanutils' 来帮助自动化:
For instance there is a method describe(Object bean)
which will return a map of all the readable attributes (ie, getters).
例如,有一种方法describe(Object bean)
将返回所有可读属性(即 getter)的映射。
Then iterate that map and call:
然后迭代该地图并调用:
setSimpleProperty(Object bean, String name, Object value)
and
和
public static Object getSimpleProperty(Object bean, String name)
And although I agree with the other poster than getters/setters are pretty trivial - I think it is still worth testing them - to eliminate typos, test property change listeners etc.
尽管我同意另一张海报,但 getter/setter 非常微不足道——我认为仍然值得测试它们——以消除拼写错误、测试属性更改侦听器等。
For example, this will dynamically extract the getters of a bean:
例如,这将动态提取 bean 的 getter:
import java.io.Serializable;
import java.util.Set;
import org.apache.commons.beanutils.PropertyUtils;
public class MyTestBean implements Serializable {
private int a;
private int b;
private int c;
private String x;
private String y;
private String z;
public static void main(String[] args) throws Exception {
MyTestBean bean=new MyTestBean();
Set prop=PropertyUtils.describe(bean).keySet();
for (Object o : prop) {
System.out.println((String)o);
}
}
public int getA() {
return a;
}
public void setA(int a) {
this.a = a;
}
public int getB() {
return b;
}
public void setB(int b) {
this.b = b;
}
public int getC() {
return c;
}
public void setC(int c) {
this.c = c;
}
public String getX() {
return x;
}
public void setX(String x) {
this.x = x;
}
public String getY() {
return y;
}
public void setY(String y) {
this.y = y;
}
public String getZ() {
return z;
}
public void setZ(String z) {
this.z = z;
}}
You will need to download both BeanUtils and CommonsLogging and both libraries' JARs to your project to run this code.
您需要将 BeanUtils 和 CommonsLogging 以及两个库的 JAR 下载到您的项目中才能运行此代码。
回答by Brian Agnew
If you have 100 fields in a class (with corresponding setters/getters) I suspect your object model is not decomposed correctly. 100+ fields sounds like an extraordinary number of fields for an object, and I would guess that it has several responsibilities that can be split across a number of more specialised objects.
如果您在一个类中有 100 个字段(带有相应的 setter/getter),我怀疑您的对象模型没有正确分解。100 多个字段听起来像是一个对象的异常数量的字段,我猜它有几个职责可以拆分到许多更专业的对象中。
回答by Franck Valentin
I guess this library is the answer to your question: http://outsidemybox.github.com/testUtils/
我想这个库是你问题的答案:http: //outsidemybox.github.com/testUtils/
it tests all the bean's initial values, the setters, the getters, hashCode(), equals() and toString(). All you have to do is define a map of default and non default property/value.
它测试所有 bean 的初始值、setter、getter、hashCode()、equals() 和 toString()。您所要做的就是定义默认和非默认属性/值的映射。
It can also test objects that are beans with additional non default constructors.
它还可以测试具有额外非默认构造函数的 bean 对象。