单测 填充测试pojo工具类

直接上


import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.ReflectUtil;
import com.google.common.collect.Lists;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.*;
import java.util.*;

/**
 * description 填充属性
 *
 * @author xuhb
 * @since 2024-04-07
 **/
@Slf4j
public class FillTestValueUtils {

    public static <T> void initializeFieldsShallow(T obj) {
        initializeFields(obj, false);
    }

    public static <T> void initializeFields(T obj, boolean isDeeply) {
        Class<?> clazz = obj.getClass();
        int modifiers = clazz.getModifiers();
        boolean isFinal = Modifier.isFinal(modifiers);
        if (isFinal) {
            return;
        }
        Field[] fields = ReflectUtil.getFields(clazz);
        for (Field field : fields) {
            field.setAccessible(true);
            Class<?> fieldType = field.getType();
            Object value = null;
            try {
                value = field.get(obj);
            } catch (IllegalAccessException e) {
                log.error("获取变量错误", e);
            }
            if (fieldType == String.class) {
                if (value != null && !"".equals(value)) {
                    continue;
                }
            }else if (fieldType == List.class) {
                if (value != null&&! ((List<?>)value).isEmpty()) {
                    continue;
                }
            } else if (value != null) {
                continue;
            }

            if (isPrimitiveOrWrapper(fieldType)) {
                if (fieldType == int.class || fieldType == Integer.class) {
                    try {
                        field.set(obj, RandomUtil.randomInt());
                    } catch (IllegalAccessException e) {
                        log.error("赋值错误", e);
                    }
                } else if (fieldType == long.class || fieldType == Long.class) {
                    try {
                        field.set(obj, RandomUtil.randomLong(1, 50000));
                    } catch (IllegalAccessException e) {
                        log.error("赋值错误", e);
                    }
                } else if (fieldType == double.class || fieldType == Double.class) {
                    try {
                        field.set(obj, RandomUtil.randomDouble(1, 50000));
                    } catch (IllegalAccessException e) {
                        log.error("赋值错误", e);
                    }
                } else if (fieldType == float.class || fieldType == Float.class) {
                    try {
                        field.set(obj, RandomUtil.randomInt(1, 50000));
                    } catch (IllegalAccessException e) {
                        log.error("赋值错误", e);
                    }
                } else if (fieldType == boolean.class || fieldType == Boolean.class) {
                    try {
                        field.set(obj, true);
                    } catch (IllegalAccessException e) {
                        log.error("赋值错误", e);
                    }
                } else if (fieldType == byte.class || fieldType == Byte.class) {
                    try {
                        field.set(obj, (byte) RandomUtil.randomInt(1, 100));
                    } catch (IllegalAccessException e) {
                        log.error("赋值错误", e);
                    }
                } else {
                    log.info("此字段未配置:{},{}", field.getName(), fieldType);
                }
            } else if (fieldType == String.class) {
                try {
                    if (field.getName().contains("Id") || field.getName().contains("id")) {
                        field.set(obj, String.valueOf(RandomUtil.randomInt(1, 50000)));
                    } else {
                        field.set(obj, RandomUtil.randomString(5));
                    }

                } catch (IllegalAccessException e) {
                    log.error("赋值错误", e);
                }
            } else if (fieldType == Date.class) {
                try {

                    field.set(obj, new DateTime("2024-01-01"));
                } catch (IllegalAccessException e) {
                    log.error("赋值错误", e);
                }
            } else if (fieldType == List.class) {
                if (isDeeply){
                    try {
                        Type genericType = field.getGenericType();
                        if (genericType instanceof ParameterizedType) {
                            ParameterizedType type = (ParameterizedType) genericType;
                            Type[] typeArguments = type.getActualTypeArguments();
                            for (Type typeArgument : typeArguments) {
                                Class<?> typeArgClass = (Class<?>) typeArgument;
                                List<?> pojoList;
                                if (typeArgClass.getName().contains("Id") || typeArgClass.getName().contains("id")) {
                                    pojoList = getPojoList(typeArgClass, 2, true);
                                } else {
                                    pojoList = getPojoList(typeArgClass, 2);
                                }

                                field.set(obj, pojoList);
                            }
                        } else {
                            field.set(obj, Collections.emptyList());
                        }

                    } catch (IllegalAccessException e) {
                        log.error("赋值错误", e);
                    }
                }else {
                    try {
                        field.set(obj, Collections.emptyList());
                    } catch (IllegalAccessException e) {
                        log.error("赋值错误", e);
                    }
                }
            } else if (fieldType == Map.class) {
                try {
                    field.set(obj, Collections.emptyMap());
                } catch (IllegalAccessException e) {
                    log.error("赋值错误", e);
                }
            } else if (fieldType == Set.class) {
                try {
                    field.set(obj, Collections.emptySet());
                } catch (IllegalAccessException e) {
                    log.error("赋值错误", e);
                }
            } else if (fieldType == Enum.class) {
                log.info("此值为枚举{}", fieldType.getName());
            } else if (fieldType == Collection.class) {
                log.info("此值为集合{}", fieldType.getName());
            } else if (hasNoArgConstructor(fieldType)) {
                try {
                    Object o = fieldType.newInstance();
                    if (isDeeply){
                        initializeFields(o);
                    }
                    field.set(obj, o);
                } catch (IllegalAccessException | InstantiationException e) {
                    log.error("赋值错误", e);
                }
            }

        }
    }

    public static void initializeFields(Object... obj) {
        for (Object o : obj) {
            initializeFields(o);
        }
    }

    public static <T> void initializeFields(T obj) {
        initializeFields(obj, true);
    }

    public static <E> List<E> getPojoList(Class<E> clazz, int size) {
        return getPojoList(clazz, size, false, true);
    }

    public static <E> List<E> getPojoListShallow(Class<E> clazz, int size) {
        return getPojoList(clazz, size, false, false);
    }

    public static <E> List<E> getPojoList(Class<E> clazz, int size, boolean isNumberString) {
        return getPojoList(clazz, size, isNumberString, true);
    }

    public static <E> List<E> getPojoList(Class<E> clazz, int size, boolean isNumberString, boolean isDeeply) {
        List<E> list = Lists.newArrayList();
        for (int i = 0; i < size; i++) {
            E e = null;
            try {
                if (isPrimitiveOrWrapperNumber(clazz)) {
                    if (clazz == int.class || clazz == Integer.class) {
                        Integer i1 = RandomUtil.randomInt(1, 500);
                        e = (E) i1;
                    } else if (clazz == long.class || clazz == Long.class) {
                        Long i1 = RandomUtil.randomLong(1, 500);
                        e = (E) i1;
                    } else if (clazz == double.class || clazz == Double.class) {
                        Double i1 = RandomUtil.randomDouble(1, 500);
                        e = (E) i1;
                    } else if (clazz == float.class || clazz == Float.class) {
                        Float i1 = (float) RandomUtil.randomInt(1, 500);
                        e = (E) i1;
                    } else if (clazz == byte.class || clazz == Byte.class) {
                        Byte i1 = (byte) RandomUtil.randomInt(1, 100);
                        e = (E) i1;
                    }
                } else if (clazz == String.class) {
                    String a;
                    if (isNumberString) {
                        a = String.valueOf(RandomUtil.randomInt(0,500));
                    } else {
                        a = RandomUtil.randomString(5);
                    }
                    e = (E) a;
                } else if (hasNoArgConstructor(clazz)) {
                    e = clazz.newInstance();
                    if (isDeeply){
                        initializeFields(e);
                    }
                }
                if (e != null) {
                    list.add(e);
                }

            } catch (InstantiationException | IllegalAccessException ex) {
                log.error("赋值错误", ex);
            }

        }
        return list;
    }

    private static boolean isPrimitiveOrWrapper(Class<?> clazz) {
        return clazz == int.class || clazz == Integer.class ||
                clazz == long.class || clazz == Long.class ||
                clazz == double.class || clazz == Double.class ||
                clazz == float.class || clazz == Float.class ||
                clazz == boolean.class || clazz == Boolean.class ||
                clazz == byte.class || clazz == Byte.class ||
                clazz == char.class || clazz == Character.class ||
                clazz == short.class || clazz == Short.class;
    }

    private static boolean isPrimitiveOrWrapperNumber(Class<?> clazz) {
        return clazz == int.class || clazz == Integer.class ||
                clazz == long.class || clazz == Long.class ||
                clazz == double.class || clazz == Double.class ||
                clazz == float.class || clazz == Float.class ||
                clazz == byte.class || clazz == Byte.class ||
                clazz == short.class || clazz == Short.class;
    }

    public static boolean hasNoArgConstructor(Class<?> clazz) {
        try {
            // 尝试获取无参构造器
            Constructor<?> constructor = clazz.getDeclaredConstructor();
            if (!constructor.isAccessible()) {
                constructor.setAccessible(true);
            }
            return true;
        } catch (NoSuchMethodException e) {
            return false;
        }
    }
}

posted @ 2024-04-16 19:45  大哥超帅  阅读(15)  评论(0编辑  收藏  举报