Unit and integration boundaries
What you will learn
An enterprise test suite gives each failure a useful location: domain tests verify rules without infrastructure, integration tests verify cooperating components, and system tests verify the deployed boundary. Start with deterministic inputs and explicit assertions; choose the smallest test that can actually falsify the claim.
- Separate domain unit tests from framework and system tests.
- Use a deterministic clock and explicit fake behavior.
- Check properties and whether deliberately broken rules are detected.
Plain Java examples: save as Main.java in a fresh directory, then run javac --release 21 Main.java and java Main. Framework examples require the companion Maven project or are reviewed artifacts. Their predicted results are not execution evidence.
Fixed time makes expiry repeatable
public class Main { static boolean valid(java.time.Clock clock, java.time.Instant expiry) { return clock.instant().isBefore(expiry); } public static void main(String[] args) {var now = java.time.Clock.fixed(java.time.Instant.parse("2026-01-01T00:00:00Z"), java.time.ZoneOffset.UTC); System.out.println(valid(now, now.instant())); System.out.println(valid(now, now.instant().plusSeconds(1))); }}Expected output:
falsetrueExpiry at the current instant is rejected; a later expiry is accepted.
Choose evidence by failure mode
A mock returns configured answers and can verify calls; a fake implements a small usable substitute; a stub supplies values. None demonstrates actual persistence. JUnit assertions are active regardless of JVM assertion flags, unlike Java assert. Use fresh fixtures and arrange, act, assert. Verify important outcomes and side effects, not every incidental interaction.
Properties and mutation testing
A property is an invariant across many inputs, such as a reservation never increasing stock. Fixed seeds make generated counterexamples reproducible; tools such as jqwik also shrink failures. PIT changes code and reruns tests; a surviving mutant may indicate a weak assertion or an equivalent change. Coverage counts executed code, not correctness. The hand-written mutation below is not a PIT report.
Seeded properties catch a wrong rule
public class Main { static int reserve(int stock,int n) { return stock-n; } public static void main(String[] args) {var rng = new java.util.Random(46); int checked = 0; boolean killed = false; for (int i=0; i<200; i++) { int stock=rng.nextInt(100), n=rng.nextInt(stock+1); if (reserve(stock,n) != stock-n) throw new AssertionError("seed 46 case "+i); if (stock+n != stock-n) killed=true; checked++; } System.out.println(checked); System.out.println(killed); }}Expected output:
200trueThe deliberately changed addition rule is detected; only valid reservations are generated, so separate invalid-input cases are needed.
Domain JUnit assertions
Source-reviewed, NOT RUN. File lab-fixtures/enterprise-testing-v46/src/test/java/lab/CatalogUnitTest.java; use the companion lab README.
package lab;import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.*;class CatalogUnitTest { @Test void tenantIsolation() { var catalog = new Catalog(); assertEquals(4, catalog.find("alpha", "pen").orElseThrow().quantity()); assertTrue(catalog.find("beta", "pen").isEmpty()); }}Predicted behavior: Both allowed and missing tenant cases pass.
The service is instantiated without a Spring context.
Mockito verifies a collaborator boundary
Source-reviewed, NOT RUN. File lab-fixtures/enterprise-testing-v46/src/test/java/lab/ControllerUnitTest.java; use the companion lab README.
package lab;import java.util.Optional;import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.*;import static org.mockito.Mockito.*;class ControllerUnitTest { @Test void missingItem() { var catalog = mock(Catalog.class); when(catalog.find("beta", "pen")).thenReturn(Optional.empty()); var response = new CatalogController(catalog).find("beta", "pen"); assertEquals(404, response.getStatusCode().value()); verify(catalog).find("beta", "pen"); }}Predicted behavior: The missing item maps to 404 and one lookup.
A stubbed lookup cannot prove actual tenant authorization.
Common mistakes
Do not replace assertions with logging or mark tests passed because they compile. Do not verify private implementation order unless the order is itself part of the contract.
Practice
Implement an expiry predicate and verify the instant before expiry, at expiry and after expiry using a fixed clock.
Expected output:
truefalsefalseCompare with the manual review criteria.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Unit and integration boundaries — knowledge check
Enterprise testing reasoning.
Which test proves a real database constraint?
Why inject a Clock?
What does a surviving mutant suggest?
A repository port is replaced by a small working in-memory implementation for tests. What kind of double is it?
Why record a seed?