mcpField
J
java-testing

java-testing

github.com/pluginagentmarketplaceテスト・品質41904 回インストール42 週間前に更新
リポジトリ

java-testing について

Java 開発者向けのテストスキルです。JUnit 5、Mockito、AssertJ、Spring Boot Test、Testcontainers を使った単体・統合テストや、TDD、カバレッジの手法を扱います。

主な機能

  1. 01JUnit 5 のテストパターン
  2. 02Mockito のモックと検証
  3. 03AssertJ のアサーション
  4. 04Spring Boot の統合テスト
  5. 05Testcontainers のセットアップ
  6. 06TDD とカバレッジ戦略

活用シーン

  1. 01Java の単体テスト作成
  2. 02依存関係のモックと呼び出し検証
  3. 03API とデータベースのテスト
  4. 04不安定または失敗するテストの調査

インストール

コマンドをコピーしてターミナルで実行します。

$npx skills add https://github.com/pluginagentmarketplace/custom-plugin-java --skill java-testing

インストール後、Claude Code で/java-testingを入力して読み込まれたか確認します。

同梱ファイル

SKILL.md と一緒にインストールされる参考資料やスクリプト。

assets/
config.yaml
schema.json
references/
GUIDE.md
PATTERNS.md
scripts/
validate.py

詳細

Java テストスキル

最新のテスト手法を用いて、Java アプリケーションの包括的なテストを作成します。

概要

このスキルでは、JUnit 5、Mockito、AssertJ を用いた Java のテストと、Spring Boot Test および Testcontainers を用いた統合テストを扱います。TDD のパターンとテストカバレッジ戦略も含まれます。

このスキルを使用する場面

次のような場合に使用します。

  • JUnit 5 でユニットテストを作成する
  • Mockito でモックを作成する
  • Testcontainers で統合テストを構築する
  • TDD/BDD の手法を実践する
  • テストカバレッジを向上させる

対象トピック

JUnit 5
  • @Test、@Nested、@DisplayName
  • ソースを指定した @ParameterizedTest
  • ライフサイクルアノテーション
  • 拡張機能とカスタムアノテーション
Mockito
  • @Mock、@InjectMocks、@Spy
  • スタブ化(when/thenReturn)
  • 検証(verify、times)
  • BDD スタイル(given/willReturn)
AssertJ
  • 流れるように記述できるアサーション
  • コレクションのアサーション
  • 例外のアサーション
  • カスタムアサーション
統合テスト
  • @SpringBootTest のスライステスト
  • Testcontainers のセットアップ
  • API 向けの MockMvc
  • データベースのテスト

クイックリファレンス

// Unit Test with Mockito
@ExtendWith(MockitoExtension.class)
class UserServiceTest {

    @Mock
    private UserRepository userRepository;

    @InjectMocks
    private UserService userService;

    @Test
    @DisplayName("Should find user by ID")
    void shouldFindUserById() {
        // Given
        User user = new User(1L, "John");
        given(userRepository.findById(1L)).willReturn(Optional.of(user));

        // When
        Optional<User> result = userService.findById(1L);

        // Then
        assertThat(result)
            .isPresent()
            .hasValueSatisfying(u ->
                assertThat(u.getName()).isEqualTo("John"));
        then(userRepository).should().findById(1L);
    }
}

// Parameterized Test
@ParameterizedTest
@CsvSource({
    "[email protected], true",
    "invalid-email, false",
    "'', false"
})
void shouldValidateEmail(String email, boolean expected) {
    assertThat(validator.isValid(email)).isEqualTo(expected);
}

// Integration Test with Testcontainers
@Testcontainers
@SpringBootTest
class OrderRepositoryIT {

    @Container
    static PostgreSQLContainer<?> postgres =
        new PostgreSQLContainer<>("postgres:15");

    @DynamicPropertySource
    static void configure(DynamicPropertyRegistry registry) {
        registry.add("spring.datasource.url", postgres::getJdbcUrl);
        registry.add("spring.datasource.username", postgres::getUsername);
        registry.add("spring.datasource.password", postgres::getPassword);
    }

    @Autowired
    private OrderRepository repository;

    @Test
    void shouldPersistOrder() {
        Order saved = repository.save(new Order("item", 100.0));
        assertThat(saved.getId()).isNotNull();
    }
}

// API Test with MockMvc
@WebMvcTest(UserController.class)
class UserControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @MockBean
    private UserService userService;

    @Test
    void shouldReturnUser() throws Exception {
        given(userService.findById(1L))
            .willReturn(Optional.of(new User(1L, "John")));

        mockMvc.perform(get("/api/users/1"))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.name").value("John"));
    }
}

テストデータビルダー

public class UserTestBuilder {
    private Long id = 1L;
    private String name = "John Doe";
    private String email = "[email protected]";
    private boolean active = true;

    public static UserTestBuilder aUser() {
        return new UserTestBuilder();
    }

    public UserTestBuilder withName(String name) {
        this.name = name;
        return this;
    }

    public UserTestBuilder inactive() {
        this.active = false;
        return this;
    }

    public User build() {
        return new User(id, name, email, active);
    }
}

// Usage
User user = aUser().withName("Jane").inactive().build();

カバレッジ目標


<configuration>
    <rules>
        <rule>
            <element>BUNDLE</element>
            <limits>
                <limit>
                    <counter>LINE</counter>
                    <value>COVEREDRATIO</value>
                    <minimum>0.80</minimum>
                </limit>
            </limits>
        </rule>
    </rules>
</configuration>

トラブルシューティング

よくある問題
問題原因解決策
モックが動作しない@ExtendWith がないMockitoExtension を追加する
テストで NPE が発生するモックが初期化されていない@InjectMocks を確認する
不安定なテスト状態が共有されているテストデータを分離する
コンテキストの読み込みに失敗するBean が不足している@MockBean を使用する
デバッグチェックリスト
□ Run single test in isolation
□ Check mock setup matches invocation
□ Verify @BeforeEach setup
□ Review @Transactional boundaries
□ Check for shared mutable state

使用方法

Skill("java-testing")

関連スキル

  • java-testing-advanced - 高度なパターン
  • java-spring-boot - Spring のスライステスト

コメント