0
0
JUnittesting~20 mins

Deterministic tests in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Deterministic Testing Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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);
    }
}
ATest fails because the random number generated is not 42
BTest throws NullPointerException
CTest fails due to AssertionError with number 0
DTest passes because the random number generated is 42
Attempts:
2 left
💡 Hint
Check what number Random(12345L).nextInt(100) actually produces.
assertion
intermediate
1: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?
AassertEquals(System.currentTimeMillis(), methodUnderTest())
BassertTrue(methodUnderTest() > 0)
CassertNotNull(methodUnderTest())
DassertEquals(1672531200000L, methodUnderTest())
Attempts:
2 left
💡 Hint
Deterministic tests require fixed expected values.
🔧 Debug
advanced
2: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);
    }
}
ARandom object is not seeded, causing non-deterministic output
BassertTrue is used incorrectly and should be assertEquals
CTest method is missing @BeforeEach annotation
DRandom.nextBoolean() always returns false
Attempts:
2 left
💡 Hint
Consider how randomness affects test repeatability.
🧠 Conceptual
advanced
1:30remaining
Why are deterministic tests important in automated testing?
Select the best reason why deterministic tests are preferred in automated test suites.
AThey do not require assertions
BThey require less code to write
CThey always produce the same result, making failures easier to diagnose
DThey run faster than non-deterministic tests
Attempts:
2 left
💡 Hint
Think about test reliability and debugging.
framework
expert
3: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?
ACall the real external service every test run
BUse Mockito to mock the external service and return a fixed date
CUse Thread.sleep() to wait for the service response
DIgnore the external service call in the test
Attempts:
2 left
💡 Hint
Think about controlling external dependencies in tests.