RandomThrottleGate implements ThrottleGate { @Override public boolean isOpen() { double chance = randomNumberGenerator.nextDouble(); boolean result = chance <= throttleGateLikelihood; log.debug(String.format("Generated random number %.2f, which is %s than throttleGateLikelihood (%.2f). ThrottleGate is %s.", chance, (result ? "less" : "greater"), throttleGateLikelihood, (result ? "OPEN" : "CLOSED"))); return result; } RandomThrottleGate(double throttleGateLikelihood); @Override boolean isOpen();  }
@Test public void testNeverFullyOpenOrFullyClosed() { int openGateCounter = (int) IntStream.range(0, MAX_ITERATIONS).filter(i -> this.throttleGateUnderTest.isOpen()).count(); String message = "Gate opened " + openGateCounter + "/" + MAX_ITERATIONS + " times " + "(expecting at least " + MIN_OPEN_GATES + " and a maximum of " + MAX_OPEN_GATES + ")"; isBetween(openGateCounter, MIN_OPEN_GATES, MAX_OPEN_GATES, message); System.out.println(message); }