CtrlK
BlogDocsLog inGet started
Tessl Logo

testing-java

Java testing with JUnit 5 and Mockito: test lifecycle, mocking, and integration test patterns

75

1.14x
Quality

63%

Does it follow best practices?

Impact

92%

1.14x

Average score across 3 eval scenarios

SecuritybySnyk

Passed

No known issues

Optimize this skill with Tessl

npx tessl skill review --optimize ./skills/testing-java/SKILL.md
SKILL.md
Quality
Evals
Security

Testing — Java

Framework

  • JUnit 5 (@Test, @BeforeEach, @AfterEach, @ParameterizedTest).
  • Mockito for mocking.
  • Run: mvn test / gradle test

Test Structure

@ExtendWith(MockitoExtension.class)
class OrderServiceTest {

    @Mock
    private PaymentGateway paymentGateway;

    @InjectMocks
    private OrderService orderService;

    @Test
    void shouldThrowOrderExceptionWhenPaymentFails() {
        // Arrange
        when(paymentGateway.charge(any())).thenThrow(new PaymentException("Declined"));

        // Act & Assert
        assertThrows(OrderException.class, () -> orderService.placeOrder(testOrder()));
    }
}

Parameterized Tests

@ParameterizedTest
@ValueSource(strings = {"", " ", "\t"})
void shouldReturnTrueForBlankStrings(String value) {
    assertTrue(StringUtils.isBlank(value));
}

Mocking Rules

  • Use @Mock and @InjectMocks with MockitoExtension — avoid Mockito.mock() in tests.
  • Verify critical interactions: verify(paymentGateway, times(1)).charge(expectedAmount).
  • Use @Captor to capture and assert arguments passed to mocks.

Integration Tests

  • Annotate with @SpringBootTest for full context; @WebMvcTest for controller slice only.
  • Use an in-memory DB (H2) for repository tests.
  • Separate integration tests into src/integrationTest/ and run in a separate Gradle task.

Coverage

  • Target 80%+ line coverage on service/domain layers.
  • Use JaCoCo: gradle jacocoTestReport and enforce with jacocoTestCoverageVerification.
Repository
ucdavis/ai-skills-registry
Last updated
Created

Is this your skill?

If you maintain this skill, you can claim it as your own. Once claimed, you can manage eval scenarios, bundle related skills, attach documentation or rules, and ensure cross-agent compatibility.