基础unitTest生成

本文主要探讨了如何在Java项目中进行单元测试的自动化生成,包括使用相关工具和库进行unit test的创建和执行,旨在提升开发效率和代码质量。
摘要由CSDN通过智能技术生成
import org.springframework.util.ResourceUtils;
import org.springframework.util.StringUtils;

import java.io.*;
import java.lang.annotation.Annotation;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Pattern;

public class GenerateUnitTestJava {
   

    public static void main(String[] args) throws IOException {
   
        Path.PROJECT_NAME = "";
        Delete.delete();
        List<ClassTemplate> l = Scan.scan();
        for (ClassTemplate t : l) {
   
            Write.write(t);
        }
    }

    private static class Path {
   
        static String PROJECT_NAME = "";
        static String find() throws FileNotFoundException {
   
            return String.format("%s/%s", ResourceUtils.getFile("").getAbsolutePath(), PROJECT_NAME);
        }
    }

    private static class Scan {
   
        static Pattern JAVA_END = Pattern.compile(".+\\.java$");
        static Pattern PACKAGE_PT = Pattern.compile(".*package .*;.*");
        static Pattern STATIC_CLASS_PT = Pattern.compile(".*[ ]+static[ ]+class[ ]+.*");
        static Pattern CLASS_PT = Pattern.compile(".*[ ]+class[ ]+.*");
        static Pattern ENUM_PT = Pattern.compile(".*[ ]+enum[ ]+.*");
        static String[] SP_ARR = {
   "{", "<", " implements ", " extends "};

        static List<ClassTemplate> scan() throws IOException {
   
            return scan(Path.find() + "/src/main/java/");
        }

        static List<ClassTemplate> scan(String filePath) throws IOException {
   
            return scan(new File(filePath));
        }

        static List<ClassTemplate> scan(File f) throws IOException {
   
            if (null == f) {
   
                return Collections.emptyList();
            }
            if (f.isDirectory()) {
   
                File[] files = f.listFiles();
                if (files.length < 1) {
   
                    return Collections.emptyList();
                }
                List<ClassTemplate> r = new ArrayList<>();
                for (File f0 : files) {
   
                    r.addAll(scan(f0));
                }
                return r;
            }
            String fn = f.getName();
            if (!JAVA_END.matcher(fn).matches()) {
   
                return Collections.emptyList();
            }
            List<String> cns = findClassName(f);
            List<ClassTemplate> r = new ArrayList<>();
            for (String cn : cns) {
   
                try {
   
                    Class<?> tClass = Class.forName(cn);
                    int mds = tClass.getModifiers();
                    if (tClass.isInterface() || Modifier.isPrivate(mds)
                            || (Modifier.isAbstract(mds) && !tClass.isEnum())) {
   
                        continue;
                    }
                    r.add(Builder.buildTemplate(tClass));
                } catch (Throwable throwable) {
   
                    // default
                }
            }
            return r;
        }

        static List<String> findClassName(File f) throws IOException {
   
            List<String> r = new ArrayList<>();
            BufferedReader br = new BufferedReader(new FileReader(f));
            String t;
            while (null != (t=br.readLine()) && !PACKAGE_PT.matcher(t).matches()) {
   
                // default;
            }
            if (null == t) {
   
                br.close();
                return Collections.emptyList();
            }
            int ps = t.indexOf("package ");
            String pn = t.substring(ps + 8, t.indexOf(";",ps));
            String fn = f.getName().substring(0, f.getName().length() - 5);
            r.add(String.format("%s.%s", pn, fn));
            while (null != (t=br.readLine())) {
   
                if (t.length() < 7) {
   
                    continue;
                }
                findNormalClass(r, t, pn, fn);
                findStaticClass(r, t, pn, fn);
                findNormalEnum(r, t, pn, fn);
            }
            br.close();
            return r;
        }

        static String getJName(String t, int f) {
   
            int i = t.length();
            for (String s : SP_ARR) {
   
                int si = t.indexOf(s, f);
                i = si > 0 && si < i ? si : i;
            }
            return t.substring(f, i).trim();
        }

        private static void findNormalEnum(List<String> r, String t, String pn, String fn) {
   
            if (!ENUM_PT.matcher(t).matches()) {
   
                return;
            }
            String cn = getJName(t, t.indexOf(" enum ") + 6);
            if (cn.equals(fn)) {
   
                return;
            }
            r.add(String.format("%s.%s", pn, cn));
            r.add(String.format("%s.%s$%s", pn, fn, cn));
        }

        private static void findStaticClass(List<String> r, String t, String pn, String fn) {
   
            if(!STATIC_CLASS_PT.matcher(t).matches
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值