Challenge - 5 Problems
Deterministic Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a deterministic JUnit test with fixed seed
Consider the following JUnit test that uses a fixed seed for randomness. What will be the output when this test runs?
JUnit
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import java.util.Random; public class RandomTest { @Test void testRandomNumber() { Random rand = new Random(12345L); int number = rand.nextInt(100); assertEquals(59, number); } }
Attempts:
2 left
💡 Hint
Check what number Random(12345L).nextInt(100) actually produces.
✗ Incorrect
The Random object with seed 12345L generates a deterministic sequence. The first nextInt(100) call returns 59, not 42, so the assertion fails.
❓ assertion
intermediate1:30remaining
Choosing the correct assertion for deterministic output
You have a method that returns the current timestamp in milliseconds. Which assertion ensures the test remains deterministic?
Attempts:
2 left
💡 Hint
Deterministic tests require fixed expected values.
✗ Incorrect
Only option D uses a fixed expected value, making the test deterministic. Options A and C depend on current time and can vary. Option D only checks non-null but not exact value.
🔧 Debug
advanced2:30remaining
Debugging flaky test caused by non-deterministic behavior
This JUnit test intermittently fails. What is the most likely cause?
JUnit
import static org.junit.jupiter.api.Assertions.*; import org.junit.jupiter.api.Test; import java.util.Random; public class FlakyTest { @Test void testRandomBoolean() { Random rand = new Random(); boolean flag = rand.nextBoolean(); assertTrue(flag); } }
Attempts:
2 left
💡 Hint
Consider how randomness affects test repeatability.
✗ Incorrect
Without a fixed seed, Random produces different values each run, causing the test to fail sometimes when flag is false.
🧠 Conceptual
advanced1:30remaining
Why are deterministic tests important in automated testing?
Select the best reason why deterministic tests are preferred in automated test suites.
Attempts:
2 left
💡 Hint
Think about test reliability and debugging.
✗ Incorrect
Deterministic tests produce consistent results, so when a test fails, the cause is easier to find and fix.
❓ framework
expert3:00remaining
Ensuring deterministic behavior in JUnit tests using mocking
You want to test a method that calls an external service returning the current date. Which approach ensures deterministic tests?
Attempts:
2 left
💡 Hint
Think about controlling external dependencies in tests.
✗ Incorrect
Mocking the external service to return a fixed date ensures the test output is predictable and repeatable.