Write focused, efficient tests that validate core functionality without unnecessary complexity. Key principles: 1. Test one concept per case 2. Use appropriate test scope (unit vs integration)
Write focused, efficient tests that validate core functionality without unnecessary complexity. Key principles:
Instead of exhaustive parameterized tests:
@ParameterizedTest
@ValueSource(ints = { -1, 0, 100 })
void testValues(int value) {
// Testing simple pass-through logic
}
Prefer focused single test:
@Test
void testValue() {
// Test with representative value
}
For JSON validation, use specialized assertions:
// Instead of string contains checks:
assertThat(entity.getBody()).contains("\"name\":\"test\"");
// Use structured assertions:
JsonContent body = new JsonContent(entity.getBody());
assertThat(body).extractingPath("name").isEqualTo("test");
When testing output, prefer CapturedOutput over complex mocks:
@Test
void testOutput(CapturedOutput output) {
// Trigger output
assertThat(output).contains("expected message");
}
Reserve integration tests for validating system interactions that cannot be effectively tested at the unit level.
Enter the URL of a public GitHub repository