JavaParser抽取测试用例对应的被测方法

背景介绍

博主目前要做的工作需要将一个java项目的所有RD手写的测试用例和被测方法对应起来,最后将得到的结果存入一个json文件。

本教程以项目GitHub - binance/binance-connector-java 为例。

结果展示

最终会得到一个 funcTestMap.json,里面存放着focal func(被测方法)和对应的test case.

实现原理

  1. 解析被测项目的src\main目录下的源码,将每一个java源文件中的方法声明(MethodDeclaration)存进一个map,其中key是方法名,value是完整方法题。

  2. 解析被测项目的src\test\java\unit目录下的测试文件,对于每一个测试文件中的方法声明,如果方法名以"test"开头,则认为是测试用例,进入下一步。

  3. 解析每一个测试用例,获取该测试用例的调用的所有方法MethodCallExpr,如果该方法存在第一步所得到的map当中,则认为该方法是测试用例的被测方法,和测试用例一起存入结果json数组中。

局限性

没有考虑到overload的情况。由于是根据方法名称而不是名称+参数类型作为方法的标识符。在存入阶段,遇到重载的方法,后面进的会覆盖掉之前的;在召回阶段,仅仅使用方法名称来查找方法体,有一定的几率找到错误的被测方法。

完整代码

项目结构如下

JsonCreator.java

java 复制代码
package org.example;

import com.github.javaparser.ast.body.MethodDeclaration;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.util.Map;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;

public class JsonCreator {
    public static void createAndSaveJson1(Map<String, MethodDeclaration> map, String saveJsonPath) {
        JsonArray jsonArray = new JsonArray();
        for (Map.Entry<String, MethodDeclaration> entry : map.entrySet()) {
            JsonObject keyValue = new JsonObject();
            keyValue.addProperty("key", entry.getKey());
            keyValue.addProperty("value", String.valueOf(entry.getValue()));
            jsonArray.add(keyValue);
        }

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String jsonOutput = gson.toJson(jsonArray);

        try {
            Files.write(Paths.get(saveJsonPath), jsonOutput.getBytes(StandardCharsets.UTF_8));
            System.out.println("JSON file saved successfully to " + saveJsonPath);
        } catch (Exception e) {
            System.err.println("Error writing JSON to file: " + e.getMessage());
        }
    }

    public static void createAndSaveJson2(Map<MethodDeclaration, MethodDeclaration> map, String saveJsonPath) {
        JsonArray jsonArray = new JsonArray();
        for (Map.Entry<MethodDeclaration, MethodDeclaration> entry : map.entrySet()) {
            JsonObject keyValue = new JsonObject();
            keyValue.addProperty("focal func", String.valueOf(entry.getKey()));
            keyValue.addProperty("test case", String.valueOf(entry.getValue()));
            jsonArray.add(keyValue);
        }

        Gson gson = new GsonBuilder().setPrettyPrinting().create();
        String jsonOutput = gson.toJson(jsonArray);

        try {
            Files.write(Paths.get(saveJsonPath), jsonOutput.getBytes(StandardCharsets.UTF_8));
            System.out.println("JSON file saved successfully to " + saveJsonPath);
        } catch (Exception e) {
            System.err.println("Error writing JSON to file: " + e.getMessage());
        }
    }

}

TestMethodAnalyzer.java

java 复制代码
package org.example;

import com.github.javaparser.ParseResult;
import com.github.javaparser.ast.CompilationUnit;
import com.github.javaparser.ast.body.MethodDeclaration;
import com.github.javaparser.ast.comments.JavadocComment;
import com.github.javaparser.ast.expr.MethodCallExpr;
import com.github.javaparser.ast.visitor.VoidVisitorAdapter;
import com.github.javaparser.utils.SourceRoot;

import java.io.IOException;
import java.nio.file.Paths;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class TestMethodAnalyzer {

    private static final Map<String, MethodDeclaration> methodMap = new HashMap<>();
    private static final Map<MethodDeclaration, MethodDeclaration> funcTestMap = new HashMap<>();

    public static void main(String[] args) throws IOException {
        // Load and index all Java files from the main source directory
        SourceRoot mainSourceRoot = new SourceRoot(Paths.get("C:\\dataset\\binance-connector-java\\src\\main"));
        mainSourceRoot.tryToParseParallelized().forEach(TestMethodAnalyzer::indexMethodDeclarations);

        System.out.println("The size of the map is: " + methodMap.size());
        String saveJsonPath1 = "C:\\codes\\Java\\extract_func_test_pair\\src\\main\\java\\org\\methodMap.json";
        JsonCreator.createAndSaveJson1(methodMap,saveJsonPath1);

        // Now parse the test files
        SourceRoot testSourceRoot = new SourceRoot(Paths.get("C:\\dataset\\binance-connector-java\\src\\test\\java\\unit"));
        testSourceRoot.tryToParseParallelized().forEach(TestMethodAnalyzer::analyzeTestClass);

        System.out.println("The size of the map is: " + funcTestMap.size());
        String saveJsonPath2 = "C:\\codes\\Java\\extract_func_test_pair\\src\\main\\java\\org\\funcTestMap.json";
        JsonCreator.createAndSaveJson2(funcTestMap,saveJsonPath2);
    }

    private static void indexMethodDeclarations(ParseResult<CompilationUnit> result) {
        result.ifSuccessful(cu -> cu.findAll(MethodDeclaration.class).forEach(md -> {
            md.setJavadocComment((JavadocComment) null);
            methodMap.put(md.getNameAsString(), md);
        }));
    }

    public static void analyzeTestClass(ParseResult<CompilationUnit> result) {
        result.ifSuccessful(cu -> {
            cu.accept(new VoidVisitorAdapter<Void>() {
                @Override
                public void visit(MethodDeclaration md, Void arg) {
                    super.visit(md, arg);
                    if (md.getNameAsString().startsWith("test")) {
                        analyzeTestMethods(md);
                    }
                }
            }, null);
        });
    }

    private static void analyzeTestMethods(MethodDeclaration md) {
        List<MethodCallExpr> methodCalls = md.findAll(MethodCallExpr.class);
        for (MethodCallExpr call : methodCalls) {
            MethodDeclaration method = methodMap.get(call.getNameAsString());

            if (method != null) {
                System.out.println("Test Method: ");
                System.out.println(md);
                System.out.println("Calls Method body:");
                method.setJavadocComment((JavadocComment)null);
                System.out.println(method);
                System.out.println("---------------------------------------");

                funcTestMap.put(method,md);
            }
        }


    }

}

maven依赖

只有两个(位于pom.xml)

XML 复制代码
<dependencies>
    <dependency>
        <groupId>com.google.code.gson</groupId>
        <artifactId>gson</artifactId>
        <version>2.10.1</version>
    </dependency>
    <dependency>
        <groupId>com.github.javaparser</groupId>
        <artifactId>javaparser-symbol-solver-core</artifactId>
        <version>3.26.0</version>
    </dependency>
</dependencies>
相关推荐
Helix2501 天前
JDK单元测试实战:JUnit 5与Mockito注解、断言、参数化测试、Mock注入完整指南
java·junit·单元测试·junit 5·mockito·mockmvc·参数化测试
JudithHuang3 天前
Vue3 Vitest 单元测试进阶
前端·单元测试·log4j·vue
JAVA面经实录9173 天前
AI 辅助写单元测试
java·人工智能·单元测试·ai编程
泛联新安3 天前
Omni Unittest单元测试智能体,AI时代的软件质量门禁
单元测试·汽车软件·比亚迪
慧都产品智能化4 天前
汽车软件年底自查:按安全影响排定缺陷整改顺序
c语言·c++·测试工具·单元测试·汽车·代码规范
回眸&啤酒鸭4 天前
【回眸】AI 大模型驱动 Tessy 单元测试集成效果全景展示
网络·人工智能·单元测试
维爱智VR认知学能5 天前
VR学能软件离线断线与乱序事件测试:确定性到达表与重放
python·单元测试·软件工程·分布式系统·离线同步
维爱智VR认知学能5 天前
VR客户端异常诊断包白名单脱敏与受控字段构建实现
python·单元测试·软件工程·数据安全·日志脱敏
维爱智VR认知学能6 天前
Python SQLite本地训练记录表迁移失败回滚与事务验证
数据库·python·单元测试·sqlite·软件工程
慧都产品智能化10 天前
汽车软件工具鉴定:范过度扩张带来的测试与文档成本
c++·测试工具·单元测试·代码复审