在 2021 年回答这个问题,因为这个问题仍然存在。
如果您的 DevOps 管道对 repos 施加覆盖限制,Bean 会添加到代码库中,并且会产生非常负面的影响。有两种方法可以克服它。
排除豆类(我会说不应该这样做)。
为 bean 编写测试用例(这是我们作为开发人员可以做的最可悲的事情来浪费我们的时间:()。
在大多数情况下,您最终会为 bean 编写测试用例。
我编写了这个使用反射的简单实用程序/测试用例,可以让您增加 Junit 代码覆盖率并节省您的时间。
待测豆:城市
package com.test.beans;
import java.util.List;
/**
* @author ameena
*
*/
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;
}
}
自动化 bean 测试的类
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;
/**
* @author ameena
*
*/
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);
}
}
没什么特别的,但它为我节省了为 23 个 bean 编写测试用例 :)
问候阿米特·米娜