# Testing Java

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

- **Type:** Skill
- **Install:** `agentstack add skill-ucdavis-ai-skills-registry-testing-java`
- **Verified:** Yes — security-reviewed for prompt injection and unsafe behavior
- **Seller:** [ucdavis](https://agentstack.voostack.com/s/ucdavis)
- **Installs:** 0
- **Category:** [Agent Skills](https://agentstack.voostack.com/c/agent-skills)
- **Latest version:** 0.1.0
- **License:** MIT
- **Upstream author:** [ucdavis](https://github.com/ucdavis)
- **Source:** https://github.com/ucdavis/ai-skills-registry/tree/main/skills/testing-java

## Install

```sh
agentstack add skill-ucdavis-ai-skills-registry-testing-java
```

Requires the [AgentStack CLI](https://agentstack.voostack.com/docs/cli). Works with Claude Code, Cursor, and any MCP-compatible agent.

## About

# Testing — Java

## Framework
- **JUnit 5** (`@Test`, `@BeforeEach`, `@AfterEach`, `@ParameterizedTest`).
- **Mockito** for mocking.
- Run: `mvn test` / `gradle test`

## Test Structure
```java
@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
```java
@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`.

## Source & license

This open-source skill is cataloged on AgentStack and links to its original source — we do not rehost the code.

- **Author:** [ucdavis](https://github.com/ucdavis)
- **Source:** [ucdavis/ai-skills-registry](https://github.com/ucdavis/ai-skills-registry)
- **License:** MIT

Install and usage instructions live in the source repository linked above.

## Pricing

- **Free** — Free

## Security capabilities

Automated source analysis of v0.1.0 — what this tool can access:

- **Network access:** no
- **Filesystem access:** no
- **Shell / process execution:** no
- **Environment & secrets:** no
- **Dynamic code execution:** no

*"Yes" means the capability is present in the source — more access means more to trust, not that it is unsafe.*


## Versions

- **0.1.0** — security scan: passed — Imported from the upstream source.

## Links

- Listing page: https://agentstack.voostack.com/l/skill-ucdavis-ai-skills-registry-testing-java
- Seller: https://agentstack.voostack.com/s/ucdavis
- Browse the marketplace: https://agentstack.voostack.com/browse

---
Listed on AgentStack — the marketplace for AI agent skills and MCP servers. Every listing is security-reviewed. Creators keep 70%.
