Testing async code, architecture rules & coverage
Awaitility for eventual outcomes, testing events and outboxes, Spring Modulith verify, ArchUnit rules, JaCoCo coverage and what coverage does not tell you.
Testing asynchronous behaviour
Thread.sleep(2000) makes tests either slow or flaky. Awaitility polls until a condition holds, or times out:
@Test
void submittingReport_notifiesApproversAfterCommit() {
reportService.submit(reportId);
await().atMost(Duration.ofSeconds(10))
.pollInterval(Duration.ofMillis(200))
.untilAsserted(() ->
assertThat(mailpit.messagesTo("approver@unit.edu")).hasSize(1));
}
pom.xml (awaitility) · integration testsAwaitility is used for RabbitMQ consumers, after-commit listeners and SSE pushes.
EmailRetryIT · EmailRestartIT · spring-modulith-starter-testThe email outbox tests assert eventual delivery to Mailpit, and survival across a context restart. Spring Modulith’s test support can also assert that events were published (Scenario and published-events APIs).
Architecture tests
Rules like “the domain must not import web code” or “modules only talk through their APIs” decay over time unless something enforces them.
src/test/.../ModularityTests.java · ArchitectureTest.java@Test void verifiesModularStructure() {
ApplicationModules.of(SperotekAccountingApplication.class).verify(); // no illegal cross-module deps, no cycles
}This is complemented by an ArchitectureTest (ArchUnit-style rules). Modulith can also generate documentation, as C4/PlantUML diagrams of the modules, with new Documenter(modules).writeDocumentation().
// ArchUnit examples
@ArchTest
static final ArchRule domainIsPure = noClasses().that().resideInAPackage("..domain..")
.should().dependOnClassesThat().resideInAnyPackage("..web..", "org.springframework.web..");
@ArchTest
static final ArchRule servicesDontReturnEntitiesToControllers = /* ... */ null;
@ArchTest
static final ArchRule noFieldInjection = noFields().should().beAnnotatedWith(Autowired.class);
Coverage with JaCoCo
pom.xml (JaCoCo 0.8.16)JaCoCo is configured to measure line and branch coverage across unit and integration tests.
Coverage tells you what was executed, not what was verified. A test with no assertions can still reach 100% coverage. Use coverage to find untested areas, not as a goal by itself. Many teams gate on around 70–80% for changed lines.
Other test types to know
| Type | Tool | What it catches |
|---|---|---|
| Contract tests | Pact, Spring Cloud Contract | API drift between frontend and backend |
| Performance / benchmark | ReportBenchmarkIT (SperoTek), JMH, k6, Gatling |
Regressions in speed |
| Mutation | PIT | Weak assertions |
| Property-based | jqwik | Edge cases (“any balanced journal stays balanced after reversal”) |
| E2E | Playwright (frontend + API) | Whole user flows |
| Security | OWASP ZAP, dependency scanning (Dependabot, mvn dependency-check) |
Known vulnerabilities |
infrastructure/test/infrastructure.test.ts · frontend ProtectedRoute.test.tsxEven the infrastructure is tested: CDK assertions on the generated CloudFormation, plus frontend component tests.