Skip to content

Commit 0cf6044

Browse files
Update rule metadata
1 parent 5b96ab2 commit 0cf6044

1 file changed

Lines changed: 4 additions & 2 deletions

File tree

  • sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java

sonar-java-plugin/src/main/resources/org/sonar/l10n/java/rules/java/S8692.html

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
<p>Reading the system clock directly within unit tests introduces non-determinism. Tests that rely on the current wall-clock time are "flaky": they
22
may pass today but fail tomorrow, or behave differently depending on the time zone and local time of the CI/CD runner.</p>
33
<p>To ensure that they are repeatable and predictable, <code>.now()</code> methods like <code>LocalDate.now()</code>,
4-
<code>LocalDateTime.now()</code>, or <code>Instant.now()</code> should only be called with a fixed clock in unit tests. <code>Clock.system(ZoneId
5-
zone)</code>, <code>Clock.systemDefaultZone()</code> and <code>Clock.systemUTC()</code> should also be avoided in tests.</p>
4+
<code>LocalDateTime.now()</code>, or <code>Instant.now()</code> should only be called with a fixed clock in unit tests. Legacy APIs such as
5+
<code>System.currentTimeMillis()</code>, <code>new Date()</code>, and <code>Calendar.getInstance()</code> have the same problem and should be replaced
6+
with clock injection. Other methods that explicitly call the system clock, such as <code>Clock.system(ZoneId zone)</code>,
7+
<code>Clock.systemDefaultZone()</code> and <code>Clock.systemUTC()</code> should also be avoided in tests.</p>
68
<h2>Why is this an issue?</h2>
79
<p>When a test uses the system clock, it relies on an external, uncontrollable state. This makes it impossible to:</p>
810
<ul>

0 commit comments

Comments
 (0)