Bird
Raised Fist0
JUnittesting~20 mins

Deterministic tests in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What does it mean for a JUnit test to be deterministic?
easy
A. It always produces the same result given the same input
B. It runs faster than other tests
C. It uses random data each time it runs
D. It depends on external services to pass

Solution

  1. Step 1: Understand the meaning of deterministic

    Deterministic means something behaves the same way every time under the same conditions.
  2. Step 2: Apply this to JUnit tests

    A deterministic test always gives the same result when run with the same input and environment.
  3. Final Answer:

    It always produces the same result given the same input -> Option A
  4. Quick Check:

    Deterministic = Same result every time [OK]
Hint: Deterministic means repeatable results every run [OK]
Common Mistakes:
  • Confusing deterministic with fast execution
  • Thinking random data makes tests deterministic
  • Assuming external services guarantee determinism
2. Which of the following is the correct way to write a deterministic assertion in JUnit 5?
easy
A. assertNull(getRandomObject());
B. assertTrue(random.nextBoolean());
C. assertFalse(externalServiceCall());
D. assertEquals(expected, actual);

Solution

  1. Step 1: Identify deterministic assertions

    Deterministic assertions compare known expected values to actual results.
  2. Step 2: Check each option

    assertEquals(expected, actual); compares expected and actual values directly, which is deterministic. Options A, B, and D depend on random or external calls, which are not deterministic.
  3. Final Answer:

    assertEquals(expected, actual); -> Option D
  4. Quick Check:

    Deterministic assertions compare fixed expected and actual values [OK]
Hint: Use fixed expected values in assertions for determinism [OK]
Common Mistakes:
  • Using random values inside assertions
  • Calling external services in assertions
  • Assuming assertNull is always deterministic
3. Consider this JUnit test snippet:
int result = calculator.add(2, 3);
assertEquals(5, result);

What will be the test result if the add method always returns the sum of its inputs?
medium
A. Test will fail because 2 + 3 is not 5
B. Test will pass because the result matches expected
C. Test will fail due to syntax error
D. Test will be flaky and sometimes pass

Solution

  1. Step 1: Understand the add method behavior

    The add method returns the sum of inputs, so add(2, 3) returns 5.
  2. Step 2: Check the assertion

    The assertion expects 5 and the actual result is 5, so the assertion passes.
  3. Final Answer:

    Test will pass because the result matches expected -> Option B
  4. Quick Check:

    Expected 5, got 5 = pass [OK]
Hint: If expected equals actual, test passes [OK]
Common Mistakes:
  • Thinking 2 + 3 is not 5
  • Assuming syntax error without checking code
  • Believing test is flaky without randomness
4. This JUnit test sometimes fails:
@Test
void testRandomNumber() {
  int num = new Random().nextInt(10);
  assertTrue(num == 0);
}

What is the main reason this test is not deterministic?
medium
A. The test depends on random values causing different results
B. The test uses a new Random object each time
C. The assertion condition is incorrect
D. The test method is missing @BeforeEach annotation

Solution

  1. Step 1: Identify randomness in the test

    The test generates a random number each run, so output varies.
  2. Step 2: Understand impact on determinism

    Because the number changes, the test result can vary, making it non-deterministic.
  3. Final Answer:

    The test depends on random values causing different results -> Option A
  4. Quick Check:

    Random values cause non-determinism [OK]
Hint: Random values cause flaky tests, avoid them [OK]
Common Mistakes:
  • Thinking assertion condition is wrong
  • Believing new Random() fixes determinism
  • Confusing test annotations with determinism
5. You want to make this test deterministic:
@Test
void testCurrentTime() {
  long time = System.currentTimeMillis();
  assertTrue(time > 0);
}

Which approach best ensures determinism?
hard
A. Remove the assertion to avoid failures
B. Add Thread.sleep(1000) before assertion
C. Replace System.currentTimeMillis() with a fixed mock time value
D. Run the test multiple times and average results

Solution

  1. Step 1: Identify non-deterministic part

    System.currentTimeMillis() returns changing time, causing test to vary.
  2. Step 2: Apply mocking for fixed value

    Replacing it with a fixed mock value makes the test produce the same result every run.
  3. Final Answer:

    Replace System.currentTimeMillis() with a fixed mock time value -> Option C
  4. Quick Check:

    Fixed mock values ensure determinism [OK]
Hint: Mock changing values to fixed ones for deterministic tests [OK]
Common Mistakes:
  • Using sleep does not fix changing values
  • Removing assertions hides problems, not fix determinism
  • Averaging results does not guarantee same output
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard