Easy-Peasy POJO Testing in Java

Mahesh Phutane
3 min readJan 12, 2024

--

Hey there, Java devs! 🚀 Today, let’s talk about something super cool—unit testing our Plain Old Java Objects (POJOs) with a sprinkle of reflection magic and the power of bean descriptors to reduce your code coverage tension like nothing. It’s like giving our POJOs a high-five to make sure they’re rocking it in the code world.🌟

Why Bother Testing POJOs?

Our POJOs are the unsung heroes holding our data. Testing them might sound like overkill, but trust me, it’s crucial. We want to make sure their fields are on point, getters and setters are smooth operators, and any custom moves they’ve got are the talk of the town.

Note: We have to do this for sonar :)

Let’s consider that we have to test the below POJO, which has large numbers of attributes and respective getter and setter methods.

public class YourPojo{

private String arg1;
private Date arg2;
:
:
// Large numbers of attributes

public void setArg1(String arg1){
this.arg1 = arg1;
}
:
:
// Getter and setter for all the Attributes

}

This can be tested in two easiest ways without manually testing each getter-setter as below:

  1. Using Reflection

We will directly jump into the code, where we will see how reflection saves us.

import org.junit.jupiter.api.Test;
import org.springframework.util.StringUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Date;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class YourPojoTestUsingReflection {

private YourPojo obj;

@SuppressWarnings("unchecked")
@Test
public void testYourPojo(){
try {
obj = (YourPojo.class).getDeclaredConstructor().newInstance();
Field[] fields = YourPojo.class.getDeclaredFields();
for(Field field : fields){

String fieldName = field.getName();
String name = StringUtils.capitalize(fieldName);
String getter = "get" + name;
String setter = "set" + name;

Object valueToSet = null;
Class<?> dataType = field.getType();
if (dataType.isAssignableFrom(String.class)) {
valueToSet = "str";
} else if (dataType.isAssignableFrom(Long.class) || dataType.isAssignableFrom(long.class)) {
valueToSet = Long.valueOf("1");
} else if (dataType.isAssignableFrom(Integer.class) || dataType.isAssignableFrom(int.class)) {
valueToSet = Integer.valueOf("1");
} else if (dataType.isAssignableFrom(Double.class) || dataType.isAssignableFrom(double.class)) {
valueToSet = Double.valueOf("1.0");
} else if (dataType.isAssignableFrom(Date.class)) {
valueToSet = new Date();
} else if (dataType.isAssignableFrom(Boolean.class)) {
valueToSet = Boolean.FALSE;
}
Method getterMethod = YourPojo.class.getMethod(getter);
Method setterMethod = YourPojo.class.getMethod(setter, getterMethod.getReturnType());
setterMethod.invoke(obj, valueToSet);
Object result = getterMethod.invoke(obj);
assertEquals(result, valueToSet);
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

2. Using Bean Descriptor


import org.junit.jupiter.api.Test;

import java.beans.*;
import java.lang.reflect.Method;

import static org.junit.jupiter.api.Assertions.assertEquals;

public class YourPojoTestUsingBeanDescriptor {
YourPojo pojo1 = new YourPojo();
YourPojo pojo2 = new YourPojo();
@Test
public void testYourPojo() throws IntrospectionException {
// Create a BeanInfo object for the POJO class you want to test.
BeanInfo beanInfo = Introspector.getBeanInfo(YourPojo.class);

// Get the property descriptors for the POJO class.
PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
// Iterate through the property descriptors and test the getter and setter methods for each property.
for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
try {
Method getterMethod = propertyDescriptor.getReadMethod();
Method setterMethod = propertyDescriptor.getWriteMethod();

Class<?> dataType = propertyDescriptor.getPropertyType();
Object valueToSet = null;
if(setterMethod!=null){
if(dataType.isAssignableFrom(String.class)){
valueToSet = "DUMMY_VAL";
}else if(dataType.isAssignableFrom(Integer.class)){
valueToSet = 1;
} else if (dataType.isAssignableFrom(Boolean.class)) {
valueToSet = Boolean.TRUE;
} else if (dataType.isAssignableFrom(Double.class)) {
valueToSet = 100.0d;
}
setterMethod.invoke(pojo1,valueToSet);
setterMethod.invoke(pojo2,valueToSet);
}
if(getterMethod!=null){
Object obj1 = getterMethod.invoke(pojo1);
Object obj2 = getterMethod.invoke(pojo2);
assertEquals(obj1,obj2);
}

} catch (Exception e) {
throw new RuntimeException(e);
}

}
}
}

--

--