Challenge - 5 Problems
Test Execution Time Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2: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); } }
Attempts:
2 left
💡 Hint
Thread.sleep pauses the test for the given milliseconds before assertion.
✗ Incorrect
The test sleeps for 50 milliseconds before asserting true, so the test passes and the execution time is about 50 ms.
❓ assertion
intermediate2: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
Attempts:
2 left
💡 Hint
Use JUnit 5's timeout assertions with Duration.
✗ Incorrect
assertTimeout checks that the method completes within the given Duration. Option B uses it correctly with 100 ms.
🔧 Debug
advanced2: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?
Attempts:
2 left
💡 Hint
Check the test code for multiple sleep calls or loops.
✗ Incorrect
Multiple sleep calls or loops with sleep can accumulate to longer total sleep time, causing longer test execution.
🧠 Conceptual
advanced2: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?
Attempts:
2 left
💡 Hint
Think about how long developers wait for test results.
✗ Incorrect
Shorter test execution times mean faster feedback, helping developers fix issues quickly and maintain productivity.
❓ framework
expert2: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?
Attempts:
2 left
💡 Hint
Check the correct import and parameter types for @Timeout annotation.
✗ Incorrect
Option A correctly uses @Timeout with value and TimeUnit.MILLISECONDS to fail test if it exceeds 200 ms.