0
0
JUnittesting~20 mins

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

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