$120 tested Claude codes · real before/after data · Full tier $15 one-timebuy --sheet=15 →
$Free 40-page Claude guide — setup, 120 prompt codes, MCP servers, AI agents. download --free →
clskills.sh — terminal v2.4 — 2,347 skills indexed● online
[CL]Skills_
JavabeginnerNew

Java Testing

Share

Set up JUnit 5 with Mockito and test containers

Works with OpenClaude

You are a Java testing expert. The user wants to set up JUnit 5 with Mockito and TestContainers to write isolated, containerized integration tests.

What to check first

  • Run java -version to confirm Java 11+ is installed (JUnit 5 requires Java 8+, but TestContainers works best on 11+)
  • Check your pom.xml or build.gradle to see current test dependencies and JUnit version

Steps

  1. Add JUnit 5 (Jupiter), Mockito, and TestContainers dependencies to your pom.xml inside the <dependencies> section with <scope>test</scope>
  2. Create a test class annotated with @ExtendWith(MockitoExtension.class) to enable Mockito annotation processing
  3. Use @Mock annotation to create mock objects and @InjectMocks to inject them into the class under test
  4. For containerized tests, add @Testcontainers class annotation and declare a @Container static field with a GenericContainer or specific image (e.g., PostgreSQL, Redis)
  5. Use lifecycle callbacks @BeforeEach to start containers and set up test data before each test method
  6. Write test methods annotated with @Test that use Mockito.when() and verify() for assertions on mock behavior
  7. For database containers, extract connection details from the running container using container.getHost() and container.getFirstMappedPort()
  8. Run tests with Maven (mvn test) or Gradle (gradle test) — JUnit 5 will auto-discover and execute all @Test methods

Code

package com.example.integration;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
import static org.mockito.Mockito.*;
import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
@Testcontainers
class UserRepositoryIntegrationTest {

    @Container
    static PostgreSQLContainer<?> postgres = new PostgreSQLContainer<>("postgres:15")
            .withDatabaseName("testdb")
            .withUsername("testuser")
            .withPassword("testpass");

    @Mock
    private EmailService emailService;

    @InjectMocks
    private UserRepository userRepository;

    private String dbUrl;
    private int dbPort;

    @BeforeEach

Note: this example was truncated in the source. See the GitHub repo for the latest full version.

Common Pitfalls

  • Treating this skill as a one-shot solution — most workflows need iteration and verification
  • Skipping the verification steps — you don't know it worked until you measure
  • Applying this skill without understanding the underlying problem — read the related docs first

When NOT to Use This Skill

  • When a simpler manual approach would take less than 10 minutes
  • On critical production systems without testing in staging first
  • When you don't have permission or authorization to make these changes

How to Verify It Worked

  • Run the verification steps documented above
  • Compare the output against your expected baseline
  • Check logs for any warnings or errors — silent failures are the worst kind

Production Considerations

  • Test in staging before deploying to production
  • Have a rollback plan — every change should be reversible
  • Monitor the affected systems for at least 24 hours after the change

Quick Info

CategoryJava
Difficultybeginner
Version1.0.0
AuthorClaude Skills Hub
javajunitmockito

Install command:

curl -o ~/.claude/skills/java-testing.md https://claude-skills-hub.vercel.app/skills/java/java-testing.md

Related Java Skills

Other Claude Code skills in the same category — free to download.

Want a Java skill personalized to YOUR project?

This is a generic skill that works for everyone. Our AI can generate one tailored to your exact tech stack, naming conventions, folder structure, and coding patterns — with 3x more detail.