Bird
Raised Fist0
JUnittesting~20 mins

Test execution time analysis in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Test Execution Time Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
JUnit test execution time output
What will be the output of the following JUnit 5 test when run?
JUnit
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

class TimeTest {
    @Test
    void fastTest() throws InterruptedException {
        Thread.sleep(50);
        assertTrue(true);
    }
}
ATest fails due to assertion error
BTest passes instantly with 0 ms execution time
CTest passes with execution time approximately 50 ms
DTest throws InterruptedException and fails
Attempts:
2 left
💡 Hint
Thread.sleep pauses the test for the given milliseconds before assertion.
❓ assertion
intermediate
2:00remaining
Assertion to check test execution time under limit
Which assertion correctly verifies that a method completes within 100 milliseconds using JUnit 5?
JUnit
import static org.junit.jupiter.api.Assertions.*;
import java.time.Duration;

// Assume methodToTest() is the method to check
AassertTrue(methodToTest() < 100);
BassertTimeout(Duration.ofMillis(100), () -> methodToTest());
CassertEquals(100, methodToTest());
DassertTimeoutPreemptively(Duration.ofSeconds(1), () -> methodToTest());
Attempts:
2 left
💡 Hint
Use JUnit 5's timeout assertions with Duration.
🔧 Debug
advanced
2:00remaining
Identify why test execution time is longer than expected
A JUnit test with Thread.sleep(10) runs for 500 ms instead of about 10 ms. What is the most likely cause?
AThe test method contains multiple Thread.sleep calls adding up to 500 ms
BJUnit automatically adds 490 ms delay to all tests
CThe test is running on a very slow machine causing Thread.sleep to last longer
DThread.sleep(10) actually sleeps for 500 ms due to JVM bug
Attempts:
2 left
💡 Hint
Check the test code for multiple sleep calls or loops.
🧠 Conceptual
advanced
2:00remaining
Understanding impact of test execution time on CI pipelines
Why is it important to monitor and limit test execution time in continuous integration (CI) pipelines?
ATo prevent tests from running on developer machines
BTo increase the number of tests run in parallel regardless of duration
CTo ensure tests always fail if they run longer than 1 second
DTo reduce feedback time and improve developer productivity
Attempts:
2 left
💡 Hint
Think about how long developers wait for test results.
❓ framework
expert
2:00remaining
Configuring JUnit 5 to fail tests exceeding time limit
Which JUnit 5 annotation and parameter combination will cause a test to fail if it runs longer than 200 milliseconds?
A
@Test
@Timeout(value = 200, unit = java.util.concurrent.TimeUnit.MILLISECONDS)
void testMethod() {}
B
@Test
@Timeout(200)
void testMethod() {}
C
@Test
@Timeout(value = 200)
void testMethod() {}
D
@Test
@Timeout(value = 200, unit = java.time.temporal.ChronoUnit.MILLIS)
void testMethod() {}
Attempts:
2 left
💡 Hint
Check the correct import and parameter types for @Timeout annotation.

Practice

(1/5)
1. What is the main purpose of measuring test execution time in JUnit?
easy
A. To automatically fix failing tests
B. To check if tests are written with correct syntax
C. To verify the correctness of test assertions
D. To identify slow tests and improve test suite performance

Solution

  1. Step 1: Understand test execution time concept

    Measuring execution time helps find tests that take longer than expected.
  2. Step 2: Identify the goal of measuring time

    Knowing slow tests allows developers to optimize or fix them, improving overall test speed.
  3. Final Answer:

    To identify slow tests and improve test suite performance -> Option D
  4. Quick Check:

    Execution time = find slow tests [OK]
Hint: Execution time helps spot slow tests to speed up runs [OK]
Common Mistakes:
  • Confusing execution time with test correctness
  • Thinking it fixes tests automatically
  • Ignoring performance impact of slow tests
2. Which JUnit code snippet correctly measures the execution time of a test method?
easy
A. long start = System.nanoTime(); // test code here long end = System.nanoTime(); System.out.println(end - start);
B. long start = System.currentTimeMillis(); // test code here long end = System.currentTimeMillis(); System.out.println(end - start);
C. int start = System.nanoTime(); // test code here int end = System.nanoTime(); System.out.println(end - start);
D. int start = System.currentTimeMillis(); // test code here int end = System.currentTimeMillis(); System.out.println(end - start);

Solution

  1. Step 1: Check correct data type for time

    System.nanoTime() returns long, so variables must be long, not int.
  2. Step 2: Verify correct method usage

    System.nanoTime() is preferred for measuring elapsed time with better precision.
  3. Final Answer:

    long start = System.nanoTime(); // test code here long end = System.nanoTime(); System.out.println(end - start); -> Option A
  4. Quick Check:

    Use long + System.nanoTime() for timing [OK]
Hint: Use long type with System.nanoTime() for precise timing [OK]
Common Mistakes:
  • Using int instead of long for time variables
  • Using System.currentTimeMillis() for high precision timing
  • Mixing data types causing compile errors
3. Given the following JUnit test snippet, what will be printed?
long start = System.nanoTime();
Thread.sleep(100);
long end = System.nanoTime();
System.out.println((end - start) / 1_000_000 + " ms");
medium
A. Approximately 100 ms
B. Exactly 100 ms
C. Approximately 0 ms
D. Compilation error due to Thread.sleep

Solution

  1. Step 1: Understand Thread.sleep effect

    Thread.sleep(100) pauses the thread for about 100 milliseconds.
  2. Step 2: Calculate elapsed time in milliseconds

    Elapsed time = (end - start) in nanoseconds, divided by 1,000,000 converts to milliseconds, close to 100 ms.
  3. Final Answer:

    Approximately 100 ms -> Option A
  4. Quick Check:

    Sleep 100ms prints ~100 ms [OK]
Hint: Sleep pauses ~100ms, nanoTime difference shows ~100 ms [OK]
Common Mistakes:
  • Expecting exact 100 ms (sleep is approximate)
  • Confusing nanoseconds with milliseconds
  • Thinking Thread.sleep causes compile error
4. Identify the error in this JUnit test measuring execution time:
@Test
void testMethod() {
  long start = System.nanoTime();
  // test code
  long end = System.nanoTime();
  System.out.println(end - start);
}
medium
A. Missing exception handling for test code
B. Using System.nanoTime() is incorrect for timing
C. No error; code correctly measures time
D. Printing time inside test causes test failure

Solution

  1. Step 1: Review timing code correctness

    Using System.nanoTime() before and after test code correctly measures elapsed time.
  2. Step 2: Check for syntax or runtime errors

    No syntax errors or exceptions are present; printing time is allowed and does not fail test.
  3. Final Answer:

    No error; code correctly measures time -> Option C
  4. Quick Check:

    System.nanoTime() timing code is valid [OK]
Hint: Timing code with nanoTime and print is valid [OK]
Common Mistakes:
  • Assuming print causes test failure
  • Thinking exception handling is mandatory here
  • Confusing nanoTime with incorrect timing method
5. You want to fail a JUnit test if it runs longer than 500 milliseconds. Which approach correctly implements this using execution time measurement?
hard
A. Use System.currentTimeMillis() and assert elapsed time <= 500 nanoseconds without conversion
B. Record start and end time with System.nanoTime(), then assert elapsed time <= 500_000_000 nanoseconds
C. Print execution time and manually check after test runs
D. Use Thread.sleep(500) inside the test to limit execution time

Solution

  1. Step 1: Understand time units and assertion

    System.nanoTime() returns nanoseconds; 500 ms = 500,000,000 ns.
  2. Step 2: Implement assertion on elapsed time

    Calculate elapsed = end - start; assert elapsed <= 500_000_000 to fail if too slow.
  3. Final Answer:

    Record start and end time with System.nanoTime(), then assert elapsed time <= 500_000_000 nanoseconds -> Option B
  4. Quick Check:

    Assert elapsed ≤ 500ms in nanoseconds [OK]
Hint: Convert 500ms to nanoseconds and assert elapsed ≤ that [OK]
Common Mistakes:
  • Using Thread.sleep to limit test time
  • Not converting time units correctly
  • Only printing time without assertion
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