Why is this an issue?

Randomness in test code, whether introduced intentionally to cover multiple scenarios or unintentionally through non-deterministic library functions, undermines the principles of effective testing. In most cases, randomness leads to problems, resulting in code that is unreliable and difficult to debug. Consequently, deterministic and reproducible tests are preferred, primarily for the following reasons:

This rule raises an issue when new Random() or UUID.randomUUID() are called in test code.

How to fix it

Noncompliant code example

int userAge = new Random().nextInt(42);  // Noncompliant
UUID userID = UUID.randomUUID(); // Noncompliant

Compliant solution

static final int SEED = 0x533d;
int userAge = new Random(SEED).nextInt(42);
UUID userID = UUID.fromString("00000000-000-0000-0000-000000000001");

Resources