Skip to content
Ameen Abozaid
JNR-047

Testing practices

  • 5 Lessons
  • 2 h practice (estimate)
On this page 8 topics

Overview

Topics

  • Arrange-Act-Assert
  • fixtures
  • test data
  • avoiding brittle tests

Learn first

2 h of practice (estimate)

Learning outcomes

  1. Structure tests with Arrange-Act-Assert and name them by behavior.
  2. Build test data with builders or object mothers instead of copy-paste.
  3. Identify brittle tests (time, order, randomness, over-specification) and fix them.
  4. Balance the test suite across unit, slice, and integration levels for speed and confidence.

Lessons

  1. 01Arrange-Act-Assert and naming
  2. 02Test data builders
  3. 03Flaky and brittle tests
  4. 04Shaping a healthy suite
  5. 05Demo: hunting a flaky test

Lab and exercises

Lab

JNR-047-LAB1 · Suite cleanup

Outcomes: 1–4 · Time: ~2 h Refactor the Shop test suite: builders, deterministic clocks, no order dependence; record runtime before and after. Acceptance: 10 consecutive runs green.

Exercises (assessment: test-quality review)

IDOutcomeTask
JNR-047-EX011Rename test1, placeOrderTest, and testCart into behavior names and restructure one test as Arrange-Act-Assert.
JNR-047-EX022Write a ProductBuilder with defaults and use it in three tests.
JNR-047-EX032Create an object mother with outOfStock(), expensive(), and discounted().
JNR-047-EX043Make a coupon-expiry test deterministic with an injected Clock.
JNR-047-EX053Replace a Thread.sleep(1000) with Awaitility or a synchronous call.
JNR-047-EX063List four causes of flaky tests and one fix for each.
JNR-047-EX074Given 5 unit, 40 slice, and 120 full integration tests, explain what is wrong and how to rebalance.
Show solutions
  • EX01 placingOrderReducesStock, rejectsOrderWhenCartIsEmpty, totalIncludesTax.
  • EX02 Defaults valid; aProduct().withStock(0).build().
  • EX03 static Product outOfStock() { return aProduct().withStock(0).build(); } and so on.
  • EX04 Clock.fixed(Instant.parse(...), UTC) passed into the service; assert before and after expiry by moving the clock.
  • EX05 await().atMost(2, SECONDS).until(() -> repo.count() == 1);, or make the code run synchronously in tests.
  • EX06 Time (fixed clock), order (isolated data), randomness (seed), async/network (await or stub).
  • EX07 Too many slow tests; move logic checks down to unit tests and keep a few integration journeys.
  • Common mistakes (tests coupled to implementation) are checked in the assessment review.

Quiz

Outcomes: O1 AAA and naming · O2 test data · O3 brittle and flaky tests · O4 suite balance.

A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.

  1. 1In Arrange-Act-Assert, "Act" means:O1
  2. 2Best test name:O1
  3. 3A test data builder gives:O2
  4. 4An object mother is:O2
  5. 5Which causes a flaky test?O3
  6. 6Best way to control time in tests:O3
  7. 7A brittle test:O3
  8. 8Better than Thread.sleep for async checks:O3
  9. 9A healthy suite has the most:O4
  10. 10A suite with 150 slow integration tests and 5 unit tests suffers from:O4

Practical part

Seven exercises JNR-047-EX01 to EX07 plus the lab; for the assessment, review the quality of a given test suite. Pass: quiz 8/10, at least five exercises correct, and 10 consecutive green runs in the lab.

Common mistakes

Thread.sleep in tests; tests coupled to implementation details.

Assessment

Test-quality review.

Career relevance

Test strategy at Senior and Staff levels.

Type to search levels, phases, modules and lessons.