Set up JUnit 5 with Mockito and test containers
✓Works with OpenClaudeYou 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 -versionto confirm Java 11+ is installed (JUnit 5 requires Java 8+, but TestContainers works best on 11+) - Check your
pom.xmlorbuild.gradleto see current test dependencies and JUnit version
Steps
- Add JUnit 5 (Jupiter), Mockito, and TestContainers dependencies to your
pom.xmlinside the<dependencies>section with<scope>test</scope> - Create a test class annotated with
@ExtendWith(MockitoExtension.class)to enable Mockito annotation processing - Use
@Mockannotation to create mock objects and@InjectMocksto inject them into the class under test - For containerized tests, add
@Testcontainersclass annotation and declare a@Containerstatic field with a GenericContainer or specific image (e.g., PostgreSQL, Redis) - Use lifecycle callbacks
@BeforeEachto start containers and set up test data before each test method - Write test methods annotated with
@Testthat useMockito.when()andverify()for assertions on mock behavior - For database containers, extract connection details from the running container using
container.getHost()andcontainer.getFirstMappedPort() - Run tests with Maven (
mvn test) or Gradle (gradle test) — JUnit 5 will auto-discover and execute all@Testmethods
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
Related Java Skills
Other Claude Code skills in the same category — free to download.
Spring Boot Setup
Scaffold Spring Boot application with REST API
Maven/Gradle
Configure Maven or Gradle build system for Java projects
Spring Security
Configure Spring Security with JWT and OAuth2
Spring Data JPA
Set up Spring Data JPA with repositories and entities
Java Streams
Refactor loops to Java Streams and functional patterns
Java Docker
Create optimized Docker image for Java/Spring Boot apps
Java Virtual Threads (Project Loom)
Use Java 21+ virtual threads for high-concurrency I/O without the platform-thread overhead
Java Records and Pattern Matching
Use Java 21+ records and pattern matching for cleaner data classes
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.