junit test method for getters & setters

…衆ロ難τιáo~ 提交于 2020-01-14 08:17:30

问题


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:

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...

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...

How should I configure eclipse to do this?


回答1:


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.

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?




回答2:


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.




回答3:


You could perhaps use Apache Commons 'beanutils' to help automate this:

http://commons.apache.org/beanutils/apidocs/org/apache/commons/beanutils/PropertyUtils.html#getSimpleProperty%28java.lang.Object,java.lang.String%29

For instance there is a method describe(Object bean) which will return a map of all the readable attributes (ie, getters).

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.

For example, this will dynamically extract the getters of a bean:

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.




回答4:


I guess this library is the answer to your question: 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.

It can also test objects that are beans with additional non default constructors.



来源:https://stackoverflow.com/questions/2218177/junit-test-method-for-getters-setters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!