Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Answer

Post Timeline

deleted 1 character in body
Source Link
ndc85430
  • 1.8k
  • 3
  • 15
  • 26

Clock.fixed creates a Clock whose time is, fixed at what you give it. In the first test, then, I create a clock that's fixed at two days after the item's due date:

Clock.fixed creates a Clock whose time is, fixed at what you give it. In the first test, then, I create a clock that's fixed at two days after the item's due date:

Clock.fixed creates a Clock whose time is fixed at what you give it. In the first test, then, I create a clock that's fixed at two days after the item's due date:

Rephrase rationale for clarity.
Source Link
ndc85430
  • 1.8k
  • 3
  • 15
  • 26

The simpler way to go about this is using dependency injection: instead of having the class you want to test know about the system clock, pass in the clock so that you can substitute it with one that stays fixedvary the clock you use for testing and for production.

The simpler way to go about this is using dependency injection: instead of having the class you want to test know about the system clock, pass in the clock so that you can substitute it with one that stays fixed.

The simpler way to go about this is using dependency injection: instead of having the class you want to test know about the system clock, pass in the clock so that you can vary the clock you use for testing and for production.

deleted 3 characters in body
Source Link
ndc85430
  • 1.8k
  • 3
  • 15
  • 26

The simpler way to go about this is using dependency injection: instead of having the class you want to test know about the system clock, pass in the clock so that you can substitute it with one that can staystays fixed.

import java.time.Clock;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
public class FineCalculator {
 private Clock clock;
 public FineCalculator(Clock clock) {
 this.clock = clock;
 }
 public long fineFor(LoanedItem loanedItem) {
 LocalDate today = clock.instant().atZone(ZoneOffset.UTC).toLocalDate();
 long daysOverdue = ChronoUnit.DAYS.between(loanedItem.dueOn(), today);
 if (daysOverdue < 0) {
 daysOverdue = 0;
 }
 long fine = 1 * daysOverdue;
 return fine;
 }
}
import java.time.Clock;
import java.time.LocalDate;
import java.time.ZoneOffset;
public class Main {
 public static void main(String[] args) {
 LocalDateLoanedItem dueOnitem = new LoanedItem("Screwdriver", LocalDate.of(2024, 9, 1);
 LoanedItem item = new LoanedItem("Screwdriver", dueOn);
 Clock clock = Clock.system(ZoneOffset.UTC);
 FineCalculator fineCalculator = new FineCalculator(clock);
 System.out.println("Loaned item: " + item);
 System.out.println("Fine is: " + fineCalculator.fineFor(item));
 }
}

The simpler way to go about this is using dependency injection: instead of having the class you want to test know about the system clock, pass in the clock so that you can substitute it with one that can stay fixed.

import java.time.Clock;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
public class FineCalculator {
 private Clock clock;
 public FineCalculator(Clock clock) {
 this.clock = clock;
 }
 public long fineFor(LoanedItem loanedItem) {
 LocalDate today = clock.instant().atZone(ZoneOffset.UTC).toLocalDate();
 long daysOverdue = ChronoUnit.DAYS.between(loanedItem.dueOn(), today);
 if (daysOverdue < 0) {
 daysOverdue = 0;
 }
 long fine = 1 * daysOverdue;
 return fine;
 }
}
import java.time.Clock;
import java.time.LocalDate;
import java.time.ZoneOffset;
public class Main {
 public static void main(String[] args) {
 LocalDate dueOn = LocalDate.of(2024, 9, 1);
 LoanedItem item = new LoanedItem("Screwdriver", dueOn);
 Clock clock = Clock.system(ZoneOffset.UTC);
 FineCalculator fineCalculator = new FineCalculator(clock);
 System.out.println("Loaned item: " + item);
 System.out.println("Fine is: " + fineCalculator.fineFor(item));
 }
}

The simpler way to go about this is using dependency injection: instead of having the class you want to test know about the system clock, pass in the clock so that you can substitute it with one that stays fixed.

import java.time.Clock;
import java.time.LocalDate;
import java.time.ZoneOffset;
import java.time.temporal.ChronoUnit;
public class FineCalculator {
 private Clock clock;
 public FineCalculator(Clock clock) {
 this.clock = clock;
 }
 public long fineFor(LoanedItem loanedItem) {
 LocalDate today = clock.instant().atZone(ZoneOffset.UTC).toLocalDate();
 long daysOverdue = ChronoUnit.DAYS.between(loanedItem.dueOn(), today);
 if (daysOverdue < 0) {
 daysOverdue = 0;
 }
 long fine = 1 * daysOverdue;
 return fine;
 }
}
import java.time.Clock;
import java.time.LocalDate;
import java.time.ZoneOffset;
public class Main {
 public static void main(String[] args) {
 LoanedItem item = new LoanedItem("Screwdriver", LocalDate.of(2024, 9, 1));
 Clock clock = Clock.system(ZoneOffset.UTC);
 FineCalculator fineCalculator = new FineCalculator(clock);
 System.out.println("Loaned item: " + item);
 System.out.println("Fine is: " + fineCalculator.fineFor(item));
 }
}
deleted 7 characters in body
Source Link
ndc85430
  • 1.8k
  • 3
  • 15
  • 26
Loading
Remove some words for clarity.
Source Link
ndc85430
  • 1.8k
  • 3
  • 15
  • 26
Loading
Source Link
ndc85430
  • 1.8k
  • 3
  • 15
  • 26
Loading
lang-java

AltStyle によって変換されたページ (->オリジナル) /