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>
相关推荐
꧁༺℘₨风、凌๓༻꧂3 天前
C# MES .NET Framework Winform 单元测试
单元测试·c#·.net
IMPYLH4 天前
Lua 的 pairs 函数
开发语言·笔记·后端·junit·单元测试·lua
倚肆4 天前
Spring Boot 测试注解全解:从单元测试到集成测试
spring boot·单元测试·集成测试
安冬的码畜日常4 天前
【JUnit实战3_35】第二十二章:用 JUnit 5 实现测试金字塔策略
测试工具·junit·单元测试·集成测试·系统测试·bdd·测试金字塔
码农BookSea5 天前
用好PowerMock,轻松搞定那些让你头疼的单元测试
后端·单元测试
少云清5 天前
【软件测试】5_测试理论 _软件测试分类(重点)
软件测试·单元测试·uat测试·sit测试
秃了也弱了。6 天前
testng:Java界功能强大的单元测试框架
java·单元测试·log4j
川石课堂软件测试6 天前
自动化过程中验证码的解决思路
数据库·python·功能测试·测试工具·单元测试·tomcat·自动化
十二测试录6 天前
测试用例,常见的一些问题
功能测试·单元测试·测试用例·压力测试·可用性测试
x***J3486 天前
测试驱动开发:从单元测试到集成测试
驱动开发·单元测试·集成测试