What is the purpose of timeout annotations in JUnit?
Timeout annotations in JUnit are used to specify the maximum time a test method should run. If the test exceeds this time, it fails automatically to prevent hanging or long-running tests.
Click to reveal answer
beginner
How do you apply a timeout to a single test method in JUnit 5?
You use the @Timeout annotation above the test method and specify the duration, for example: @Timeout(5) means the test must finish within 5 seconds.
Click to reveal answer
beginner
What happens if a test annotated with @Timeout exceeds the specified time?
The test fails immediately with a timeout exception, indicating it took longer than allowed.
Click to reveal answer
intermediate
Can you apply @Timeout at the class level in JUnit? What does it do?
Yes, applying @Timeout at the class level sets the timeout for all test methods in that class. Each test must complete within the specified time individually.
Click to reveal answer
intermediate
How do you specify timeout duration units in JUnit 5's @Timeout annotation?
You can specify the time unit using the 'unit' parameter, for example: @Timeout(value = 500, unit = java.util.concurrent.TimeUnit.MILLISECONDS) sets a 500 milliseconds timeout.
Click to reveal answer
What is the default time unit for the @Timeout annotation in JUnit 5 if not specified?
ASeconds
BMilliseconds
CMinutes
DHours
✗ Incorrect
If you do not specify the unit in @Timeout, it defaults to seconds.
Where can you apply the @Timeout annotation in JUnit 5?
AOnly on test methods
BOn both test methods and test classes
COnly on test classes
DOnly on setup methods
✗ Incorrect
@Timeout can be applied on both test methods and test classes to set time limits.
What happens if a test exceeds the timeout specified by @Timeout?
ATest fails with a timeout exception
BTest is skipped
CTest continues running
DTest is retried automatically
✗ Incorrect
JUnit fails the test immediately with a timeout exception if it runs longer than allowed.
Which import is required to use @Timeout in JUnit 5?
Aorg.junit.jupiter.api.TestTimeout
Borg.junit.Timeout
Corg.junit.jupiter.api.Timeout
Dorg.junit.jupiter.api.TimeLimit
✗ Incorrect
The correct import for @Timeout in JUnit 5 is org.junit.jupiter.api.Timeout.
How do you specify a timeout of 2 minutes using @Timeout?
A@Timeout(2)
B@Timeout(value = 2, unit = java.util.concurrent.TimeUnit.SECONDS)
C@Timeout(120)
D@Timeout(value = 2, unit = java.util.concurrent.TimeUnit.MINUTES)
✗ Incorrect
You specify the value and unit explicitly: @Timeout(value = 2, unit = TimeUnit.MINUTES).
Explain how timeout annotations help improve test reliability in JUnit.
Think about what happens when a test runs forever or too slow.
You got /4 concepts.
Describe how to use the @Timeout annotation to set a 10-second timeout on a test method.
Remember the default time unit if not specified.
You got /4 concepts.
Practice
(1/5)
1. What is the main purpose of the @Timeout annotation in JUnit tests?
easy
A. To mark a test as expected to fail
B. To repeat a test multiple times automatically
C. To skip a test during execution
D. To stop a test if it runs longer than a specified time limit
Solution
Step 1: Understand the role of @Timeout
The @Timeout annotation sets a maximum time limit for a test method to run.
Step 2: Identify what happens when time limit is exceeded
If the test runs longer than the specified time, it is stopped and marked as failed.
Final Answer:
To stop a test if it runs longer than a specified time limit -> Option D
Quick Check:
@Timeout stops long tests = A [OK]
Hint: Timeout means stop test if too slow [OK]
Common Mistakes:
Confusing timeout with repeating tests
Thinking timeout skips tests
Assuming timeout marks tests as expected failures
2. Which of the following is the correct way to set a timeout of 5 seconds on a JUnit 5 test method?
easy
A. @Timeout(5)
B. @Timeout(5, SECONDS)
C. @Timeout(value = 5, unit = TimeUnit.SECONDS)
D. @Timeout(value = 5, unit = SECONDS)
Solution
Step 1: Recall JUnit 5 @Timeout syntax
The @Timeout annotation requires a value and a time unit specified as an enum from java.util.concurrent.TimeUnit.
Step 2: Match correct syntax
@Timeout(value = 5, unit = TimeUnit.SECONDS) uses named parameters with value=5 and unit=TimeUnit.SECONDS, which is correct syntax.
Final Answer:
@Timeout(value = 5, unit = TimeUnit.SECONDS) -> Option C
Quick Check:
Use named params with TimeUnit enum = C [OK]
Hint: Use named params and TimeUnit enum for timeout [OK]