Skip to content
Ameen Abozaid
Planned lessonJNR-010-L06

Demo: injecting a Clock to test time-dependent logic

  • Lesson 6 of 6
  • Junior
On This Lesson 3 topics

This lesson is planned. The outline below lists the topics it will cover. The full lesson has not been published yet.

In this lesson

make "is the sale active now?" testable; prepares JNR-010-LAB1.

Draft outline details

Step by step

  1. Show PromotionService calling LocalDate.now(); the test passes only on certain days.
  2. Add a Clock bean in a @Configuration class.
  3. Inject Clock through the constructor and use LocalDate.now(clock).
  4. Planned mistake: leave one static Instant.now() in another service; the test still flakes. Fix: search for now() and replace.
  5. In the test use Clock.fixed(...) and assert both sides of the deadline.

Practice

Apply this lesson with the module's lab, exercises and quiz.

Lab and exercises Quiz

Marking a lesson complete is optional and you can undo it any time. Your progress stays in this browser.

Type to search levels, phases, modules and lessons.