Integration Testing Implementation in Java
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Integration Testing runs in O(n) time and O(n) space, in Java as in every other language.
- ✓JUnit 5 supplies org.junit.jupiter.api.Test and a static @BeforeAll unless the class is annotated @TestInstance(PER_CLASS).
- ✓Reviewers want the container held in a static field so one instance serves the whole class, AssertJ's assertThat for readable failures, test data from an object mother or builder rather than a shared constant, and no @MockBean in a class whose name ends in IT.
- ✓The Java trap: Transactional rollback quietly changes the shape of the run. One transaction wraps the whole test, which production never does: Hibernate flushes and rolls back, so a constraint violation production would hit at commit may never appear, a lazy association resolves happily inside the test and throws LazyInitializationException in production, and anything annotated REQUIRES_NEW commits for real and survives the rollback into the next test.
- ✓JVM startup plus a context refresh is measured in seconds, so context caching is the biggest single lever: keep test configurations byte-identical and every class reuses one cached context.
What Integration Testing actually is
Integration Testing in Java: what you actually reach for
Java implementation
import static org.assertj.core.api.Assertions.assertThat;
import jakarta.persistence.EntityManager;
import jakarta.persistence.PersistenceContext;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.transaction.annotation.Transactional;
import org.testcontainers.containers.PostgreSQLContainer;
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;
@Testcontainers
@SpringBootTest
@Transactional // rolled back after each test method
class OrderRepositoryIT {
@Container
static final PostgreSQLContainer<?> PG = new PostgreSQLContainer<>("postgres:16-alpine");
@DynamicPropertySource
static void datasource(DynamicPropertyRegistry registry) {
registry.add("spring.datasource.url", PG::getJdbcUrl);
registry.add("spring.datasource.username", PG::getUsername);
registry.add("spring.datasource.password", PG::getPassword);
}
@Autowired
OrderRepository orders;
@PersistenceContext
EntityManager em;
@Test
void savedOrderIsReadBack() {
Order saved = orders.save(new Order("ABC-1", 3));
em.flush();
em.clear(); // else findById returns the cached instance
assertThat(orders.findById(saved.getId()))
.get().extracting(Order::getQty).isEqualTo(3);
}
}
Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Java-specific trap
Performance in Java, beyond the O(n)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Java type or library should I use for Integration Testing?
JUnit 5 supplies org.junit.jupiter.api.Test and a static @BeforeAll unless the class is annotated @TestInstance(PER_CLASS). Real dependencies come from Testcontainers: org.testcontainers.containers.PostgreSQLContainer driven by @Testcontainers and @Container in org.testcontainers.junit.jupiter. Spring wires the container's JDBC URL in through @DynamicPropertySource, or since Spring Boot 3.1 through @ServiceConnection on the container bean. @SpringBootTest(webEnvironment = RANDOM_PORT) with TestRestTemplate or WebTestClient exercises the real HTTP stack, and @Transactional on a test method rolls back at the end by default. Flyway or Liquibase applies the production migrations.
What is the most common Java mistake when implementing Integration Testing?
Transactional rollback quietly changes the shape of the run. One transaction wraps the whole test, which production never does: Hibernate flushes and rolls back, so a constraint violation production would hit at commit may never appear, a lazy association resolves happily inside the test and throws LazyInitializationException in production, and anything annotated REQUIRES_NEW commits for real and survives the rollback into the next test. Separately, Spring caches the ApplicationContext keyed by configuration, so one extra @TestPropertySource silently builds a second context, and static state from the first leaks whenever Surefire reuses a fork.
How does Integration Testing actually perform in Java?
JVM startup plus a context refresh is measured in seconds, so context caching is the biggest single lever: keep test configurations byte-identical and every class reuses one cached context. Testcontainers reuse (withReuse(true) plus testcontainers.reuse.enable in ~/.testcontainers.properties) skips container startup between runs, with Ryuk cleaning up when reuse is off. JUnit 5 runs classes sequentially unless junit.jupiter.execution.parallel.enabled is switched on. A per-test rollback is about a millisecond; an unnecessary context rebuild is thousands. Time complexity is O(n) and space is O(n).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialStudying for this? Devin free tiers and academic plans are laid out on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.