$npx -y skills add decebals/claude-code-java --skill test-qualityWrite high-quality JUnit 5 tests with AssertJ assertions. Use when user says "add tests", "write tests", "improve test coverage", or when reviewing/creating test classes for Java code.
| 1 | # Test Quality Skill (JUnit 5 + AssertJ) |
| 2 | |
| 3 | Write high-quality, maintainable tests for Java projects using modern best practices. |
| 4 | |
| 5 | ## When to Use |
| 6 | - Writing new test classes |
| 7 | - Reviewing/improving existing tests |
| 8 | - User asks to "add tests" / "improve test coverage" |
| 9 | - Code review mentions missing tests |
| 10 | |
| 11 | ## Framework Preferences |
| 12 | |
| 13 | ### JUnit 5 (Jupiter) |
| 14 | ```java |
| 15 | import org.junit.jupiter.api.Test; |
| 16 | import org.junit.jupiter.api.DisplayName; |
| 17 | import org.junit.jupiter.api.BeforeEach; |
| 18 | import org.junit.jupiter.api.Nested; |
| 19 | import static org.assertj.core.api.Assertions.*; |
| 20 | ``` |
| 21 | |
| 22 | ### AssertJ over standard assertions |
| 23 | ✅ **Use AssertJ**: |
| 24 | ```java |
| 25 | assertThat(plugin.getState()) |
| 26 | .as("Plugin should be started after initialization") |
| 27 | .isEqualTo(PluginState.STARTED); |
| 28 | |
| 29 | assertThat(plugins) |
| 30 | .hasSize(3) |
| 31 | .extracting(Plugin::getId) |
| 32 | .containsExactly("plugin1", "plugin2", "plugin3"); |
| 33 | ``` |
| 34 | |
| 35 | ❌ **Avoid JUnit assertions**: |
| 36 | ```java |
| 37 | assertEquals(PluginState.STARTED, plugin.getState()); // Less readable |
| 38 | assertTrue(plugins.size() == 3); // Less descriptive failures |
| 39 | ``` |
| 40 | |
| 41 | ## Test Structure (AAA Pattern) |
| 42 | |
| 43 | Always use Arrange-Act-Assert pattern: |
| 44 | |
| 45 | ```java |
| 46 | @Test |
| 47 | @DisplayName("Should load plugin from valid directory") |
| 48 | void shouldLoadPluginFromValidDirectory() { |
| 49 | // Arrange - Setup test data and dependencies |
| 50 | Path pluginDir = Paths.get("test-plugins/valid-plugin"); |
| 51 | PluginLoader loader = new DefaultPluginLoader(); |
| 52 | |
| 53 | // Act - Execute the behavior being tested |
| 54 | Plugin plugin = loader.load(pluginDir); |
| 55 | |
| 56 | // Assert - Verify results |
| 57 | assertThat(plugin) |
| 58 | .isNotNull() |
| 59 | .extracting(Plugin::getId, Plugin::getVersion) |
| 60 | .containsExactly("test-plugin", "1.0.0"); |
| 61 | } |
| 62 | ``` |
| 63 | |
| 64 | ## Naming Conventions |
| 65 | |
| 66 | ### Test class names |
| 67 | ```java |
| 68 | // Class under test: PluginManager |
| 69 | PluginManagerTest // ✅ Simple, standard |
| 70 | PluginManagerShould // ✅ BDD style (if team prefers) |
| 71 | TestPluginManager // ❌ Avoid |
| 72 | ``` |
| 73 | |
| 74 | ### Test method names |
| 75 | |
| 76 | **Option 1: should_expectedBehavior_when_condition** (descriptive) |
| 77 | ```java |
| 78 | @Test |
| 79 | void should_throwException_when_pluginDirectoryNotFound() { } |
| 80 | |
| 81 | @Test |
| 82 | void should_returnEmptyList_when_noPluginsAvailable() { } |
| 83 | |
| 84 | @Test |
| 85 | void should_loadPluginsInDependencyOrder_when_multipleDependencies() { } |
| 86 | ``` |
| 87 | |
| 88 | **Option 2: Natural language with @DisplayName** (cleaner code) |
| 89 | ```java |
| 90 | @Test |
| 91 | @DisplayName("Should load all plugins from directory") |
| 92 | void loadAllPlugins() { } |
| 93 | |
| 94 | @Test |
| 95 | @DisplayName("Should throw exception when plugin descriptor is invalid") |
| 96 | void invalidPluginDescriptor() { } |
| 97 | ``` |
| 98 | |
| 99 | ## AssertJ Power Features |
| 100 | |
| 101 | ### Collection assertions |
| 102 | ```java |
| 103 | // Basic collection checks |
| 104 | assertThat(plugins) |
| 105 | .isNotEmpty() |
| 106 | .hasSize(2) |
| 107 | .doesNotContainNull(); |
| 108 | |
| 109 | // Advanced filtering and extraction |
| 110 | assertThat(plugins) |
| 111 | .filteredOn(p -> p.getState() == PluginState.STARTED) |
| 112 | .extracting(Plugin::getId) |
| 113 | .containsExactlyInAnyOrder("plugin-a", "plugin-b"); |
| 114 | |
| 115 | // All elements match condition |
| 116 | assertThat(plugins) |
| 117 | .allMatch(p -> p.getVersion() != null, "All plugins have version"); |
| 118 | ``` |
| 119 | |
| 120 | ### Exception assertions |
| 121 | ```java |
| 122 | // Basic exception check |
| 123 | assertThatThrownBy(() -> loader.load(invalidPath)) |
| 124 | .isInstanceOf(PluginException.class) |
| 125 | .hasMessageContaining("Invalid plugin descriptor"); |
| 126 | |
| 127 | // Detailed exception verification |
| 128 | assertThatThrownBy(() -> manager.startPlugin("missing-plugin")) |
| 129 | .isInstanceOf(PluginException.class) |
| 130 | .hasMessageContaining("Plugin not found") |
| 131 | .hasCauseInstanceOf(IllegalArgumentException.class) |
| 132 | .hasNoCause(); // or verify cause chain |
| 133 | |
| 134 | // With assertThatExceptionOfType (more readable) |
| 135 | assertThatExceptionOfType(PluginException.class) |
| 136 | .isThrownBy(() -> loader.load(invalidPath)) |
| 137 | .withMessageContaining("Invalid") |
| 138 | .withMessageMatching("Invalid .* descriptor"); |
| 139 | ``` |
| 140 | |
| 141 | ### Object assertions |
| 142 | ```java |
| 143 | // Extract and verify multiple properties |
| 144 | assertThat(plugin) |
| 145 | .isNotNull() |
| 146 | .extracting("id", "version", "state") |
| 147 | .containsExactly("my-plugin", "1.0", PluginState.STARTED); |
| 148 | |
| 149 | // Using method references (type-safe) |
| 150 | assertThat(plugin) |
| 151 | .extracting(Plugin::getId, Plugin::getVersion, Plugin::getState) |
| 152 | .containsExactly("my-plugin", "1.0", PluginState.STARTED); |
| 153 | |
| 154 | // Field by field comparison |
| 155 | assertThat(actualPlugin) |
| 156 | .usingRecursiveComparison() |
| 157 | .isEqualTo(expectedPlugin); |
| 158 | ``` |
| 159 | |
| 160 | ### Soft assertions (multiple checks) |
| 161 | ```java |
| 162 | @Test |
| 163 | void shouldHaveValidPluginDescriptor() { |
| 164 | SoftAssertions softly = new SoftAssertions(); |
| 165 | |
| 166 | softly.assertThat(descriptor.getId()) |
| 167 | .as("Plugin ID") |
| 168 | .isNotBlank() |
| 169 | .matches("[a-z0-9-]+"); |
| 170 | |
| 171 | softly.assertThat(descriptor.getVersion()) |
| 172 | .as("Plugin version") |
| 173 | .matches("\\d+\\.\\d+\\.\\d+"); |
| 174 | |
| 175 | softly.assertThat(descriptor.getDependencies()) |
| 176 | .as("Dependencies") |