# spring-method-agent **Repository Path**: xiaowomiaomiao/spring-method-agent ## Basic Information - **Project Name**: spring-method-agent - **Description**: 一个 java agent用于spring项目方法调用和测试 - **Primary Language**: Java - **License**: Apache-2.0 - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 6 - **Forks**: 1 - **Created**: 2024-06-28 - **Last Updated**: 2026-02-12 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Spring method agent [English Introduction](README_en.md) | [中文介绍](README_zh.md) ## 介绍 这个项目致力于帮助程序员更高效直观且**代码无侵入**地对spring项目进行方法级别的测试.
以javaagent的形式在spring项目启动的时候进行增强,这种方式对应用来说是无代码侵入的.
jar内置了一个web服务,使得开发人员可以通过浏览器直观查询并调用spring应用中的bean方法. ## 快速使用: #### 下载链接:[版本列表](https://gitee.com/xiaowomiaomiao/spring-method-agent/releases/tag/v2.0.0) #### 将以下Java启动参数添加至你的启动脚本中: ```-javaagent:{your path}/spring-method-agent-2.0.0.jar``` #### 启动后会监听8100端口,等项目完全启动好之后打开: ```localhost:8100``` #### 打开页面后在页面进行方法的调用,可以使用以下jvm参数重新指定端口 ```-Dagent.port=8080``` ## 新功能 ### Bean方法Mock支持 ### 代理现在支持模拟Bean方法的返回值。此功能允许您在不修改实际代码的情况下模拟特定的方法行为,特别适用于测试场景。 ### 配置方式: #### 添加以下JVM启动参数: ``` -Dsma.mockEnable=true -Dsma.configPath=/yourPath/sma-mockConfig ``` ### 配置文件格式: #### 配置文件使用JSON格式,结构如下: ``` { "globalEnabled": true, "rules": [ { "className": "org.example.controller.EchoController", "methodName": "echo", "conditionField": "url", "conditionValue": "hello", "mockReturnValue": "lin", "enabled": true }, { "className": "org.example.controller.EchoController", "methodName": "user", "conditionField": "user.name", "conditionValue": "bob", "mockReturnValue": { "age": 18, "name": "bob" }, "enabled": true }, { "className": "org.example.controller.EchoController", "methodName": "calculate", "mockReturnValue": 3.14, "enabled": true }, { "className": "org.example.controller.EchoController", "methodName": "getList", "mockReturnValue": [1, 2, 3], "enabled": true }, { "className": "org.example.controller.EchoController", "methodName": "isActive", "mockReturnValue": true, "enabled": true }, { "className": "org.example.controller.EchoController", "methodName": "getData", "mockReturnValue": "{ \"items\": [1,2,3], \"count\": 3 }", "enabled": true } ] } ``` ### 配置参数说明: - globalEnabled: Mock功能的全局开关 - rules: Mock规则数组,每个规则包含: - className: 完整类名 - methodName: 要Mock的方法名 - conditionField: 条件字段,用于条件Mock - conditionValue: 条件值,用于条件Mock - mockReturnValue: Mock返回值(支持多种数据类型) - enabled: 该规则是否启用