Techiio-author
Started by Natasha JonesSep 30, 2021

Open
Java bean testing framework

1 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 1 VIEWES SHARE

Is there a framework or library available that, when given a JavaBean, will "put it through its paces," i.e., test all the getters and setters, validate that a property matches the getters and setters, etc.?

1 Replies

Techiio-commentatorNilima Paul replied 4 months ago0 likes0 dislikes

Answering this in 2021 because sadly, this problem still exists.

Beans add up to the code base and have a very negative impact if your DevOps pipelines are imposing coverage restrictions on repos. There are two ways to overcome it.

Exclude the beans ( which I would say should not be done).

Write test cases for beans ( which is the most pathetic thing that we as a developer can do to waste our time :( ).

And in most cases, you will end up writing test cases for beans.

I have written this simple Utility/test case that uses reflection and can allow you to increase the Junit code coverage and save your time.

Bean under test: City

package com.test.beans;
import java.util.List;
/**
 * @authorAmeenaa
 *
 */
public class City {
    private int postOffices;
    private int jurdictaionAreas;
    private double areaInSqMeter;
    private long population;
    private List<City> neighbourCities;
    private boolean metro;
    public int getJurdictaionAreas() {
        return jurdictaionAreas;
    }
    public void setJurdictaionAreas(int jurdictaionAreas) {
        this.jurdictaionAreas = jurdictaionAreas;
    }
    public double getAreaInSqMeter() {
        return areaInSqMeter;
    }
    public void setAreaInSqMeter(double areaInSqMeter) {
        this.areaInSqMeter = areaInSqMeter;
    }
    public long getPopulation() {
        return population;
    }
    public void setPopulation(long population) {
        this.population = population;
    }
    public int getPostOffices() {
        return postOffices;
    }
    public void setPostOffices(int postOffices) {
        this.postOffices = postOffices;
    }
    public List<City> getNeighbourCities() {
        return neighbourCities;
    }
    public void setNeighbourCities(List<City> neighbourCities) {
        this.neighbourCities = neighbourCities;
    }
    public boolean isMetro() {
        return metro;
    }
    public void setMetro(boolean metro) {
        this.metro = metro;
    }
}

The class to automate the bean testing

package com.test.beans;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.jupiter.api.Assertions.fail;
import java.beans.IntrospectionException;
import java.beans.PropertyDescriptor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import org.junit.jupiter.api.Test;
/**
 * @authorAmeenaa
 *
 */
class BeanTest {
    public void invokeSetter(Object obj, String propertyName, Object variableValue)
    {
        PropertyDescriptor propDescriptor;
        try {
            propDescriptor = new PropertyDescriptor(propertyName, obj.getClass());
            Method setter = propDescriptor.getWriteMethod();
            try {
                setter.invoke(obj,variableValue);
            } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
                e.printStackTrace();
                fail(e.getMessage());
            }
        } catch (IntrospectionException e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
    }
    public Object invokeGetter(Object obj, String variableName)
    {
        Object returnValue = null;
        try {
            PropertyDescriptor pd = new PropertyDescriptor(variableName, obj.getClass());
            Method getter = pd.getReadMethod();
            returnValue = getter.invoke(obj);
        } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException | IntrospectionException e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
        return returnValue;
    }
    private <T extends Object> void validateGettersSetters(List<T> objects) {
        for (T t : objects) {
            Class<?> aClass = t.getClass();
            for (java.lang.reflect.Field field : aClass.getDeclaredFields()) {
                System.out.println(field);
                Class<?> c = field.getType();
                if (c == String.class) {
                    invokeSetter(t, field.getName(), "dummy");
                    assertEquals("dummy", (invokeGetter(t, field.getName())));
                } else if (c == Integer.class || c == int.class) {
                    invokeSetter(t, field.getName(), 1);
                    assertEquals(1, (invokeGetter(t, field.getName())));
                }else if (c == Double.class || c == double.class) {
                    invokeSetter(t, field.getName(), 1d);
                    assertEquals(1d, (invokeGetter(t, field.getName())));
                }else if (c == Long.class || c == long.class) {
                    invokeSetter(t, field.getName(), 1l);
                    assertEquals(1l, (invokeGetter(t, field.getName())));
                }else if (c == Boolean.class || c == boolean.class) {
                    invokeSetter(t, field.getName(), true);
                    assertEquals(true, (invokeGetter(t, field.getName())));
                }else if (c == List.class){
                    //Now based on your bean and filed name
                    switch(field.getName()) {
                    case "neighbourCities" :
                        invokeSetter(t, field.getName(), new ArrayList<City>());
                        assertNotNull(invokeGetter(t, field.getName()));
                        break;
                }
            }
        }
    }
    }
    @Test
    void testBean() {
        List<Object> objects = new ArrayList<>();
        objects.add(new City());
        validateGettersSetters(objects);
    }
}
You must be Logged in to reply
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance