Install
openclaw skills install @hcyyy0120/junit-test-genGenerates JUnit 5 test classes from JSON test case files. Invoke when user wants to generate Spring Boot JUnit 5 tests from JSON test cases.
openclaw skills install @hcyyy0120/junit-test-genLLM驱动的JUnit 5测试代码生成系统。读取由"Test Case Generator"生成的JSON格式测试用例,自动生成符合Spring Boot最佳实践和JUnit 5规范的测试类和测试方法。
┌─────────────────────────────────────────────────────────────┐
│ LLM 智能层 │
│ ┌─────────────────┐ ┌─────────────────────────────┐ │
│ │ 解析JSON测试用例 │ -> │ 分析测试类型和结构 │ │
│ └─────────────────┘ └─────────────────────────────┘ │
│ ┌─────────────────┐ ┌─────────────────────────────┐ │
│ │ 生成测试类模板 │ -> │ 生成测试方法实现 │ │
│ └─────────────────┘ └─────────────────────────────┘ │
│ ┌─────────────────────────────────────────────────────┐ │
│ │ 生成完整的JUnit 5测试代码 │ │
│ └─────────────────────────────────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
│
▼
┌─────────────────────────────────────────────────────────────┐
│ Python 执行层 │
│ ┌──────────────────────┐ ┌──────────────────────────┐ │
│ │ run_generator.py │ │ testcase_parser.py │ │
│ │ 入口脚本(一键执行) │ │ 测试用例JSON解析器 │ │
│ └──────────────────────┘ └──────────────────────────┘ │
│ ┌──────────────────────┐ ┌──────────────────────────┐ │
│ │ jtest_generator.py │ │ │ │
│ │ JUnit 5测试代码生成器 │ │ │ │
│ └──────────────────────┘ └──────────────────────────┘ │
└─────────────────────────────────────────────────────────────┘
{
"test_suite": "ControllerName_MethodName",
"description": "接口功能描述",
"interface_info": {
"endpoint": "/api/path",
"method": "POST",
"content_type": "application/json",
"function": "接口功能说明"
},
"request_dto": {
"field1": "类型 - 字段说明"
},
"test_cases": [
{
"id": "TC_001",
"name": "测试场景描述",
"endpoint": "/api/path",
"method": "POST",
"headers": {"Content-Type": "application/json"},
"body": {"field1": "value1"},
"setup": {"mysql": [], "redis": {}},
"expected": {"status": 200, "body": {"key": "value"}},
"teardown": {"mysql": [], "redis": []}
}
]
}
package generated.tests.controller;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.jdbc.core.JdbcTemplate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.Map;
import static org.junit.jupiter.api.Assertions.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@SpringBootTest
@AutoConfigureMockMvc
class ControllerNameMethodNameTest {
private static final Logger logger = LoggerFactory.getLogger(ControllerNameMethodNameTest.class);
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@Autowired
private JdbcTemplate jdbcTemplate;
@Test
void testMethodName_TC_001() throws Exception {
logger.info("Executing test: TC_001 - 测试场景描述");
// Setup - 准备测试数据
// jdbcTemplate.execute("INSERT INTO ...");
// Given - 构建请求
Map<String, Object> requestBody = new HashMap<>();
requestBody.put("field1", "value1");
// When & Then - 执行请求并验证
mockMvc.perform(post("/api/path")
.contentType(MediaType.APPLICATION_JSON)
.content(objectMapper.writeValueAsString(requestBody)))
.andExpect(status().isOk())
.andExpect(jsonPath("$.key").value("value"))
.andDo(print());
// Teardown - 清理测试数据
// jdbcTemplate.execute("DELETE FROM ...");
}
}
| 测试类型 | 注解 | 说明 |
|---|---|---|
| API接口测试 | @SpringBootTest @AutoConfigureMockMvc | Controller层测试 |
| 服务层测试 | @SpringBootTest | Service层测试 |
| 单元测试 | @ExtendWith(MockitoExtension.class) | 纯单元测试 |
| 集成测试 | @SpringBootTest | 完整集成测试 |
# 进入skill目录
cd .trae/skills/junit-test-generator/scripts
# 一键生成测试类
python run_generator.py <test_cases.json> [output_dir]
# 示例
python run_generator.py test_cases.json
python run_generator.py test_cases.json src/test/java
from jtest_generator import JUnitTestGenerator
generator = JUnitTestGenerator(package_name="your.package.name")
java_code = generator.generate(test_suite)
┌─────────────────────────────────────────────────────────────┐
│ 完整测试执行流程 │
├─────────────────────────────────────────────────────────────┤
│ 1. testcase-generator: 生成测试用例JSON │
│ ↓ │
│ 2. 本skill: 解析JSON并生成JUnit 5测试类 │
│ ↓ │
│ 3. 写入 src/test/java 目录 │
│ ↓ │
│ 4. mvn test [-Dtest=ClassName] 执行测试 │
│ ↓ │
│ 5. 查看 target/surefire-reports/ 测试结果 │
└─────────────────────────────────────────────────────────────┘
| 命令 | 说明 |
|---|---|
mvn test | 执行所有测试 |
mvn test -Dtest=ClassName | 执行指定测试类 |
mvn test -Dtest=ClassName#methodName | 执行指定测试方法 |
mvn test -Dspring.profiles.active=test | 使用test配置执行 |
mvn test verify | 执行测试并验证 |
target/surefire-reports/ - 测试执行报告 (XML/TXT)generated.tests.controllertest + 操作描述 + 场景描述assertEquals, assertTrue, jsonPath等你是一个测试工程师。请根据以下JSON测试用例,生成符合JUnit 5规范的Spring Boot测试代码。
## 测试套件信息
- 测试套件名: {test_suite_name}
- 描述: {description}
- 接口路径: {endpoint}
- HTTP方法: {method}
## 测试用例数据
{test_cases_json}
## 生成要求
1. 使用JUnit 5注解 (@Test, @BeforeEach, @AfterEach)
2. 使用@SpringBootTest和@AutoConfigureMockMvc进行API测试
3. 使用MockMvc进行HTTP请求模拟
4. 使用jsonPath进行响应断言
5. 在测试方法注释中包含测试用例ID和描述
6. 遵循Spring Boot最佳实践
请生成完整的Java测试类代码。
@SpringBootTest 集成测试@WebMvcTest Controller单元测试