Java Test Updater 标志

Java Test Updater

社区
ArabelaTso
java-test-updater

Update Java test classes and methods to work with new code versions after refactoring or modifications. Use when code changes break existing tests due to signature changes, refactoring, or behavior modifications. Takes old and new code versions plus old tests as input, and outputs updated tests that compile and pass against the new code. Handles method signature changes, class refactoring, assertion updates, and mock modifications.

概览

发布者ArabelaTso
代码仓库Skills-4-SE
技能名称java-test-updater
星标数
258
复刻数
25
附带文件
仅含指令
许可协议Apache-2.0
链接
  • Markdown 格式的指令

    模型按需加载的 SKILL.md 文件,因此只有在请求确实匹配时才会消耗 Token。

  • 兼容任何大语言模型

    AI 技能采用纯 Markdown,而非特定提供商的代码,因此可搭配 GPT、Claude、Gemini、Grok 或本地模型使用。

  • 指令自成一体

    模型所需的一切都包含在指令中,无需同步额外文件。

  • 开源

    由 ArabelaTso 发布于 GitHub。请在安装前查看源内容。

安装

在 TypingMind 中安装 Java Test Updater AI 技能,即可搭配任意大语言模型使用;也可以将其放入其他支持读取 SKILL.md 的智能体中。

1

安装到 TypingMind

TypingMind 可直接从 GitHub 文件夹安装技能:读取 SKILL.md,打包资源文件,并将结果保存在本地。

  1. 打开应用,前往“插件 → 技能”。
  2. 选择“从 GitHub 安装”。
  3. 粘贴下方的技能文件夹 URL 并确认。
    https://github.com/ArabelaTso/Skills-4-SE/tree/main/skills/java-test-updater
  4. 请在每个需要使用该技能的聊天中启用它。
依次选择“插件 → 技能 → 添加技能 → 从 GitHub URL 导入”,然后粘贴文件夹 URL 并点击“继续”。
2

安装到其他智能体

任何支持 Agent Skills 格式的智能体都可以使用此技能,只需将文件夹复制到该智能体的技能目录中。

Claude Code — .claude/skills
git clone --depth 1 https://github.com/ArabelaTso/Skills-4-SE.git /tmp/Skills-4-SE
mkdir -p .claude/skills
cp -r /tmp/Skills-4-SE/skills/java-test-updater .claude/skills/java-test-updater
复制后,请重启 Claude Code,以加载新技能。

在 TypingMind 中使用此技能

在任意 TypingMind 聊天中启用 Java Test Updater,后续即可交由模型处理。技能的名称和描述会放在系统提示词中,一旦请求匹配,模型就会自动加载完整指令,无需您手动调用,且在实际使用前不会消耗 Token。

一旦请求与技能匹配,模型就会自动加载 Java Test Updater。

兼容任何 AI 模型

AI 技能是纯 Markdown 指令,而非特定提供商的代码,因此 Java Test Updater 不局限于最初适配的模型。只需在 TypingMind 中安装一次,即可搭配 GPT-5、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 或您自行运行的本地模型使用,全程使用您自己的 API 密钥。

  • 仅在需要时加载

    系统提示词中仅包含技能名称和描述。首次遇到匹配的请求时才会获取指令,因此未使用的技能不会产生额外费用。

  • 在聊天中切换模型

    技能是指令而非代码,因此切换模型不会导致技能失效,新模型会读取同一个 SKILL.md。

技能指令

这是模型会加载的 SKILL.md 内容。请在安装前阅读,因为技能就是模型将遵循的指令。

Java Test Updater

Overview

Automatically update Java test code to align with changes in production code, ensuring tests compile and pass after refactoring, signature changes, or behavior modifications.

Workflow

1. Analyze Code Changes

Compare old and new versions of the production code to identify changes:

Read both versions:

  • Old code version (before changes)
  • New code version (after changes)
  • Identify the specific class and methods that changed

Categorize changes:

  • Signature changes: Parameter types, parameter order, return type, method name
  • Refactoring: Class renamed, method moved, package changed
  • Behavior changes: Logic modified, new exceptions, different return values
  • API changes: New methods added, old methods removed

Example change analysis:

java
// Old version
public double calculateDiscount(double price) {
    return price * 0.1;
}

// New version
public double calculateDiscount(double price, double discountRate) {
    if (discountRate < 0 || discountRate > 1) {
        throw new IllegalArgumentException("Invalid discount rate");
    }
    return price * discountRate;
}

Identified changes:

  • Added parameter: discountRate
  • Added validation with exception
  • Changed calculation logic

2. Analyze Existing Tests

Read and understand the old test code:

Identify test structure:

  • Test class name and package
  • Test methods and their purposes
  • Setup and teardown methods (@Before, @BeforeEach, @After, @AfterEach)
  • Test data and fixtures
  • Mocks and stubs used

Map tests to code changes:

  • Which tests call the changed methods
  • Which assertions depend on changed behavior
  • Which mocks need updating

Example old test:

java
@Test
public void testCalculateDiscount() {
    double price = 100.0;
    double result = calculator.calculateDiscount(price);
    assertEquals(10.0, result, 0.01);
}

3. Update Method Calls

Modify test code to match new method signatures:

For added parameters:

java
// Old call
calculator.calculateDiscount(price)

// Updated call
calculator.calculateDiscount(price, 0.1)  // Use appropriate default or test value

For removed parameters:

java
// Old call
calculator.calculateDiscount(price, taxRate, shippingCost)

// Updated call (if taxRate removed)
calculator.calculateDiscount(price, shippingCost)

For parameter type changes:

java
// Old call
calculator.process(userId)  // userId was int

// Updated call (if userId changed to Long)
calculator.process(Long.valueOf(userId))

For renamed methods:

java
// Old call
calculator.computeTotal()

// Updated call (if renamed to calculateTotal)
calculator.calculateTotal()

For moved methods:

java
// Old call
calculator.validateInput(data)

// Updated call (if moved to validator class)
validator.validateInput(data)

4. Update Assertions

Adjust test assertions to match new behavior:

For changed return values:

java
// Old assertion (10% fixed discount)
assertEquals(10.0, result, 0.01);

// Updated assertion (variable discount rate)
assertEquals(8.0, calculator.calculateDiscount(100.0, 0.08), 0.01);

For new exceptions:

java
// Add new test for exception
@Test
public void testInvalidDiscountRate() {
    assertThrows(IllegalArgumentException.class, () -> {
        calculator.calculateDiscount(100.0, -0.1);
    });
}

For changed behavior:

java
// Old assertion
assertTrue(result.isEmpty());

// Updated assertion (if behavior changed to return null instead)
assertNull(result);

For modified object state:

java
// Old assertion
assertEquals(1, cart.getItemCount());

// Updated assertion (if implementation changed)
assertEquals(1, cart.getItems().size());

5. Update Mocks and Stubs

Modify mock objects to match new interfaces:

For Mockito mocks with signature changes:

java
// Old mock setup
when(repository.findById(userId)).thenReturn(user);

// Updated mock setup (if return type changed to Optional)
when(repository.findById(userId)).thenReturn(Optional.of(user));

For added parameters in mocked methods:

java
// Old mock
when(service.process(data)).thenReturn(result);

// Updated mock (if parameter added)
when(service.process(data, context)).thenReturn(result);
// Or use argument matchers
when(service.process(eq(data), any(Context.class))).thenReturn(result);

For changed mock behavior:

java
// Old mock (method returned boolean)
when(validator.isValid(input)).thenReturn(true);

// Updated mock (method now throws exception instead)
doNothing().when(validator).validate(input);
// Or for invalid case:
doThrow(new ValidationException()).when(validator).validate(invalidInput);

6. Add New Test Cases

Create additional tests for new functionality or edge cases:

For new parameters:

java
@Test
public void testCalculateDiscountWithZeroRate() {
    double result = calculator.calculateDiscount(100.0, 0.0);
    assertEquals(0.0, result, 0.01);
}

@Test
public void testCalculateDiscountWithMaxRate() {
    double result = calculator.calculateDiscount(100.0, 1.0);
    assertEquals(100.0, result, 0.01);
}

For new exceptions:

java
@Test
public void testNegativeDiscountRate() {
    assertThrows(IllegalArgumentException.class, () -> {
        calculator.calculateDiscount(100.0, -0.5);
    });
}

@Test
public void testDiscountRateAboveOne() {
    assertThrows(IllegalArgumentException.class, () -> {
        calculator.calculateDiscount(100.0, 1.5);
    });
}

For new behavior:

java
@Test
public void testNewFeature() {
    // Test newly added functionality
    Result result = service.newMethod(input);
    assertNotNull(result);
    assertEquals(expectedValue, result.getValue());
}

7. Update Test Setup and Teardown

Modify test fixtures if dependencies changed:

For constructor changes:

java
// Old setup
@BeforeEach
void setUp() {
    calculator = new Calculator();
}

// Updated setup (if constructor now requires dependencies)
@BeforeEach
void setUp() {
    validator = new Validator();
    calculator = new Calculator(validator);
}

For new dependencies:

java
// Old setup
@Mock
private Repository repository;

// Updated setup (if new dependency added)
@Mock
private Repository repository;

@Mock
private CacheService cacheService;

@BeforeEach
void setUp() {
    service = new Service(repository, cacheService);
}

8. Verify Compilation

Ensure updated tests compile without errors:

Check for compilation issues:

  • Import statements are correct
  • All types are resolved
  • Method signatures match
  • No syntax errors

Common compilation fixes:

java
// Add missing imports
import java.util.Optional;
import static org.mockito.ArgumentMatchers.any;

// Fix type mismatches
Long userId = 123L;  // Instead of int userId = 123;

// Update generic types
List<String> items = new ArrayList<>();  // Instead of raw List

Compile tests:

bash
# Maven
mvn test-compile

# Gradle
./gradlew testClasses

9. Run and Verify Tests

Execute tests to ensure they pass:

Run all updated tests:

bash
# Maven
mvn test -Dtest=CalculatorTest

# Gradle
./gradlew test --tests CalculatorTest

Analyze test results:

  • All tests should pass (green)
  • No test failures
  • No test errors
  • Check test output for warnings

If tests fail:

  1. Read failure message and stack trace
  2. Identify the cause (assertion failure, exception, etc.)
  3. Fix the test code or identify issues in production code
  4. Re-run tests
  5. Repeat until all tests pass

10. Validate Test Quality

Ensure updated tests maintain quality standards:

Check test coverage:

  • New code paths are tested
  • Edge cases are covered
  • Exception handling is tested

Verify test independence:

  • Tests don't depend on execution order
  • Each test can run in isolation
  • No shared mutable state between tests

Review test clarity:

  • Test names are descriptive
  • Test logic is clear
  • Assertions are meaningful

Update Patterns

Pattern 1: Signature Change with Added Parameter

Old code:

java
public String formatName(String firstName, String lastName) {
    return firstName + " " + lastName;
}

New code:

java
public String formatName(String firstName, String lastName, String title) {
    return title + " " + firstName + " " + lastName;
}

Old test:

java
@Test
public void testFormatName() {
    String result = formatter.formatName("John", "Doe");
    assertEquals("John Doe", result);
}

Updated test:

java
@Test
public void testFormatName() {
    String result = formatter.formatName("John", "Doe", "Mr.");
    assertEquals("Mr. John Doe", result);
}

@Test
public void testFormatNameWithEmptyTitle() {
    String result = formatter.formatName("John", "Doe", "");
    assertEquals(" John Doe", result);
}

Pattern 2: Method Refactored to Different Class

Old code:

java
public class UserService {
    public boolean validateEmail(String email) {
        return email.contains("@");
    }
}

New code:

java
public class UserService {
    private EmailValidator validator;
    // validateEmail moved to EmailValidator
}

public class EmailValidator {
    public boolean isValid(String email) {
        return email.contains("@");
    }
}

Old test:

java
@Test
public void testValidateEmail() {
    assertTrue(userService.validateEmail("user@example.com"));
}

Updated test:

java
@Mock
private EmailValidator emailValidator;

@BeforeEach
void setUp() {
    userService = new UserService(emailValidator);
}

@Test
public void testEmailValidation() {
    when(emailValidator.isValid("user@example.com")).thenReturn(true);
    // Test userService method that uses emailValidator
}

// Or create separate test for EmailValidator
@Test
public void testEmailValidatorIsValid() {
    EmailValidator validator = new EmailValidator();
    assertTrue(validator.isValid("user@example.com"));
}

Pattern 3: Return Type Changed

Old code:

java
public User findUser(Long id) {
    return repository.findById(id);  // Returns User or null
}

New code:

java
public Optional<User> findUser(Long id) {
    return repository.findById(id);  // Returns Optional<User>
}

Old test:

java
@Test
public void testFindUser() {
    User user = service.findUser(123L);
    assertNotNull(user);
    assertEquals("John", user.getName());
}

Updated test:

java
@Test
public void testFindUser() {
    Optional<User> userOpt = service.findUser(123L);
    assertTrue(userOpt.isPresent());
    assertEquals("John", userOpt.get().getName());
}

@Test
public void testFindUserNotFound() {
    Optional<User> userOpt = service.findUser(999L);
    assertFalse(userOpt.isPresent());
}

Tips

  • Always read both old and new code versions completely
  • Identify all affected test methods before making changes
  • Update one test method at a time
  • Compile frequently to catch errors early
  • Run tests after each update to verify correctness
  • Maintain test coverage - don't remove tests without replacement
  • Add tests for new edge cases introduced by changes
  • Keep test names descriptive and up-to-date
  • Update test documentation/comments if behavior changed
  • Use IDE refactoring tools when available for safe renames

常见问题

Java Test Updater AI 技能有什么作用?

Update Java test classes and methods to work with new code versions after refactoring or modifications. Use when code changes break existing tests due to signature changes, refactoring, or behavior modifications. Takes old and new code versions plus old tests as input, and outputs updated tests that compile and pass against the new code. Handles method signature changes, class refactoring, assertion updates, and mock modifications.

为什么在 TypingMind 中使用 Java Test Updater?

因为您只需安装一次,就能搭配任何模型使用。Java Test Updater 采用纯 Markdown 格式,而非特定提供商专用的代码,因此同一技能可用于 GPT-5、Claude、Gemini、Grok 或本地模型,甚至可以在聊天中途切换模型,而不会影响技能正常使用。TypingMind 使用您自己的 API 密钥,因此您直接向提供商付费,而不是按席位订阅;您的技能和聊天记录也都保留在您自己的存储空间中。

如何在 TypingMind 中安装 Java Test Updater?

在 TypingMind 中打开“插件 → 技能 → 从 GitHub 安装”,然后粘贴 https://github.com/ArabelaTso/Skills-4-SE/tree/main/skills/java-test-updater。TypingMind 会读取其中的 SKILL.md,并将其安装为技能,供您在各个聊天中按需启用。

哪些 AI 模型可以使用 Java Test Updater?

您在 TypingMind 中连接的任何模型都可以。AI 技能采用纯 Markdown 指令,而非特定提供商专用的代码,因此 GPT、Claude、Gemini、Grok 和本地模型都能在请求匹配时加载此技能。

Java Test Updater 可以搭配多少个 AI 模型使用?

数量不限。只要模型支持技能,您就可以搭配 Java Test Updater 使用,包括 GPT、Claude、Gemini、Grok、DeepSeek、Mistral、Llama 等,都可在 TypingMind 中使用您自己的 API 密钥接入。

Java Test Updater AI 技能免费吗?

是的。该技能源自 ArabelaTso 在 GitHub 上以 Apache-2.0 许可证发布的项目。您只需向自己的 AI 提供商支付所用 Token 的费用。

什么是 AI 技能?

AI 技能是一组可复用的指令,用于教会 AI 模型完成某项特定任务。它遵循开放的 Agent Skills 格式:包含名称和描述的 SKILL.md 文件,以及模型可能需要的脚本、模板或参考文件。模型仅在您的请求与技能匹配时才会读取这些指令,因此已安装的技能在使用前不会产生任何费用。

AI 技能与插件或 MCP 服务器有何不同?

插件或 MCP 服务器为模型提供可调用的新工具,也就是在某处运行并返回结果的代码。AI 技能则为模型提供知识和流程:如何处理任务、遵循哪些步骤,以及怎样的输出才算优质。技能采用纯 Markdown 格式,因此不需要服务器、API 密钥或运行时环境,并且适用于任何模型。

查看全部

立即创建您自己的 AI 工作区

订阅我们的电子简报,获取新功能和后续赠奖活动的通知 👇