sonar 覆盖率自动测试pojo

package com.cfpamf.ms.insur.biz;


import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.MethodSorters;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StringUtils;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Stack;


@RunWith(SpringRunner.class)
@SpringBootTest
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class BeanAutoTest {

    @Test
    public void makeSonarHappy() throws ClassNotFoundException, SecurityException, IllegalArgumentException, URISyntaxException {

        Class<?> clz = com.cfpamf.ms.insur.biz.BootstrapApplication.class;
        URL url = clz.getResource("/");
        String rootPath = url.getPath();
        rootPath = rootPath.substring(0, rootPath.indexOf("target") + 6) + "/classes/com/cfpamf/ms/insur/biz/pojo";
        File file = new File(rootPath);

        Stack<File> stack = new Stack<>();
        stack.addAll(Arrays.asList(file));

        List<File> clazzs = new ArrayList<>();
        while (!stack.isEmpty()) {
            File f = stack.pop();
            if (f.isDirectory()) {
                File[] fs = f.listFiles();
                stack.addAll(Arrays.asList(fs));
            } else {
                clazzs.add(f);
            }
        }
        int success = 0;
        for (int i = 0, len = clazzs.size(); i < len; i++) {
            File cl = clazzs.get(i);
            String path = cl.getPath();
            path = path.substring(path.indexOf("com")).replaceAll("\\\\", ".");
            path = path.substring(path.indexOf("com")).replaceAll("/", ".");
            String className = path.substring(0, path.length() - 6);
            try {
                Class clazz = Class.forName(className);
                testClazz(clazz);
                success++;
            } catch (Exception e) {
                System.out.println("fail auto test className=" + className);
            }
        }
        System.out.println("Auto success/complete=" + success + "/" + clazzs.size());
    }

    private void testClazz(Class clz) throws IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {
        Object object1 = clz.newInstance();
        Object object2 = clz.newInstance();
        String name = "";
        String type = "";
        for (Field field : clz.getDeclaredFields()) {
            try {
                if (Modifier.isStatic(field.getModifiers())) {
                    continue;
                }
                field.setAccessible(true);
                name = StringUtils.capitalize(field.getName());
                type = field.getGenericType().toString();
                if (name.equals("$jacocoData") || type.equals("interface org.slf4j.Logger")) {
                    continue;
                }
                Class<?> clz2 = field.getType();
                Method setMethod = clz.getDeclaredMethod("set" + name, clz2);
                String getMethodName = "get" + name;
                if (type.equals("boolean")) {
                    getMethodName = "is" + name;
                }
                Method getMethod = clz.getDeclaredMethod(getMethodName);
                setMethod.invoke(object1, getMethod.invoke(object1));

                if (!clz2.isPrimitive()) {
                    setMethod.invoke(object1, clz2.newInstance());
                    getMethod.invoke(object1);
                    setMethod.invoke(object2, clz2.newInstance());
                    getMethod.invoke(object2);
                }
            } catch (Exception e) {
            }
        }
        try {
            Object object3 = clz.newInstance();
            Object object4 = clz.newInstance();
            Method equals = clz.getDeclaredMethod("equals", Object.class);
            if (equals != null) {
                equals.invoke(object1, object1);
                equals.invoke(object1, new Object());
                equals.invoke(object1, object2);
                equals.invoke(object1, object3);
                equals.invoke(object3, object4);
            }
            Method hashCode = clz.getDeclaredMethod("hashCode");
            if (hashCode != null) {
                hashCode.invoke(object1);
                hashCode.invoke(object3);
            }
            Method toString = clz.getDeclaredMethod("toString");
            if (toString != null) {
                toString.invoke(object1);
            }

            Method canEqual = clz.getDeclaredMethod("canEqual", Object.class);
            if (canEqual != null) {
                canEqual.setAccessible(true);
                canEqual.invoke(object1, clz.newInstance());
            }
        } catch (Exception ex) {
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值