Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

junit test method for getters & setters

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?

like image 867
Girish Bhatt Avatar asked Feb 07 '10 20:02

Girish Bhatt


Video Answer


4 Answers

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?

like image 56
Péter Török Avatar answered Oct 10 '22 19:10

Péter Török


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.

like image 45
Brian Agnew Avatar answered Oct 10 '22 18:10

Brian Agnew


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.

like image 42
monojohnny Avatar answered Oct 10 '22 18:10

monojohnny


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.

like image 23
Franck Valentin Avatar answered Oct 10 '22 19:10

Franck Valentin