Unit and Integration Tests
- JUnit. A JUnit test is an annotated method whose assertions define pass: run by the build on every change.
- Test scope. Unit tests isolate one class; integration tests wire real pieces: both, in a fast-to-slow pyramid.
- Naming behaviour. Name tests by behaviour: refundRejectedWhenOrderUnpaid reads as the specification it is.
Mocking and Test Design
- Test doubles. A mock stands in for a collaborator: the test scripts its answers and verifies the conversation.
- Constructor injection. Passing dependencies through constructors makes substitution trivial: testability is a design property.
- Don’t over-mock. Mock the boundaries: network, clock, database: and use real objects inside; over-mocked tests pin implementation, not behaviour.
Maintainable Java Test Suites
- Determinism. Reliable suites are deterministic: fixed clocks, seeded data, no order dependence, no real network.
- Test data builders. A builder for test objects: anOrder().paid().build(): keeps tests readable as requirements change.
- Fast feedback. A suite that runs in seconds is run always; a slow one gets skipped, then trusted, then wrong.
Before and after this level
- Before: Level 11: Networking & Data: HTTP and Network Programming, JDBC and Database Access, Reliable External Integrations.
- Next: Level 13: Modern Java: Records and Pattern Matching, Modern Language Features, Writing Idiomatic Current Java.
In the app each of these ideas is taught and then asked back, and the level ends with a checkpoint and flashcards. See Checkpoints and flashcards.
Frequently asked questions
What does Java Level 12: Testing cover?
Unit and Integration Tests, Mocking and Test Design, Maintainable Java Test Suites: 3 lessons and 9 ideas.
What should I know before this level?
The level before it, Level 11: Networking & Data, which covers HTTP and Network Programming, JDBC and Database Access, Reliable External Integrations.