Bird
Raised Fist0
JUnittesting~20 mins

Timeout annotations in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Timeout Annotation Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
JUnit Timeout Annotation Basic Behavior
What is the test result when the following JUnit 5 test method runs?
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.concurrent.TimeUnit;

public class TimeoutTest {
    @Test
    @Timeout(value = 1, unit = TimeUnit.SECONDS)
    void testTimeout() throws InterruptedException {
        Thread.sleep(1500);
    }
}
ATest passes successfully
BTest fails due to assertion error
CTest fails due to timeout exception after 1 second
DTest is skipped
Attempts:
2 left
💡 Hint
The test sleeps longer than the timeout value.
❓ assertion
intermediate
2:00remaining
Correct Usage of Timeout Annotation Units
Which of the following JUnit 5 timeout annotations correctly sets a timeout of 500 milliseconds?
A@Timeout(value = 500)
B@Timeout(value = 0.5, unit = TimeUnit.SECONDS)
C@Timeout(value = 500, unit = TimeUnit.SECONDS)
D@Timeout(value = 500, unit = TimeUnit.MILLISECONDS)
Attempts:
2 left
💡 Hint
Check the unit parameter and value type.
🔧 Debug
advanced
2:00remaining
Fixing Timeout Annotation Misuse
Given this JUnit 5 test method, what error will occur and why? @Test @Timeout(1.5) void testMethod() throws InterruptedException { Thread.sleep(1000); }
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

public class TimeoutDebug {
    @Test
    @Timeout(1.5)
    void testMethod() throws InterruptedException {
        Thread.sleep(1000);
    }
}
ATest fails due to timeout
BCompilation error: The value must be a long, not a double
CTest passes successfully
DRuntime error: IllegalArgumentException
Attempts:
2 left
💡 Hint
Check the type of the value parameter in @Timeout.
🧠 Conceptual
advanced
2:00remaining
Timeout Annotation Scope in JUnit 5
Which statement correctly describes the scope of the @Timeout annotation in JUnit 5?
A@Timeout can be applied at method and class level, affecting all test methods in the class
B@Timeout only works on individual test methods, not on classes
C@Timeout applies globally to all tests in the project without annotation
D@Timeout only works on test classes annotated with @Timeoutable
Attempts:
2 left
💡 Hint
Think about how annotations can be inherited in JUnit 5.
❓ framework
expert
2:00remaining
Behavior of Timeout with Nested Tests in JUnit 5
Consider a JUnit 5 test class with nested test classes. If @Timeout is applied on the outer class, what happens to the nested test methods?
JUnit
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.util.concurrent.TimeUnit;

@Timeout(value = 1, unit = TimeUnit.SECONDS)
public class OuterTimeoutTest {

    @Test
    void outerTest() throws InterruptedException {
        Thread.sleep(900);
    }

    @Nested
    class InnerTests {
        @Test
        void innerTest() throws InterruptedException {
            Thread.sleep(1100);
        }
    }
}
ATimeout applies to both outer and nested test methods, so innerTest fails due to timeout
BTimeout applies only to outerTest; nested tests have no timeout
CTimeout applies only to nested tests, not outerTest
DTimeout is ignored for all tests because of nesting
Attempts:
2 left
💡 Hint
Check how annotations are inherited by nested classes in JUnit 5.

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

  1. Step 1: Understand the role of @Timeout

    The @Timeout annotation sets a maximum time limit for a test method to run.
  2. 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.
  3. Final Answer:

    To stop a test if it runs longer than a specified time limit -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    @Timeout(value = 5, unit = TimeUnit.SECONDS) -> Option C
  4. Quick Check:

    Use named params with TimeUnit enum = C [OK]
Hint: Use named params and TimeUnit enum for timeout [OK]
Common Mistakes:
  • Omitting the time unit
  • Using wrong enum or missing TimeUnit prefix
  • Passing timeout as single unnamed parameter
3. Consider this JUnit 5 test method:
@Test
@Timeout(value = 2, unit = TimeUnit.SECONDS)
void testSleep() throws InterruptedException {
    Thread.sleep(3000);
}
What will happen when this test runs?
medium
A. The test will fail due to timeout after 2 seconds
B. The test will pass after sleeping 3 seconds
C. The test will fail immediately with an exception
D. The test will be skipped because of timeout

Solution

  1. Step 1: Analyze the timeout and sleep duration

    The test has a timeout of 2 seconds but sleeps for 3 seconds, which is longer than allowed.
  2. Step 2: Understand test behavior on timeout

    JUnit will stop the test after 2 seconds and mark it as failed due to timeout.
  3. Final Answer:

    The test will fail due to timeout after 2 seconds -> Option A
  4. Quick Check:

    Sleep longer than timeout causes failure = B [OK]
Hint: Sleep longer than timeout causes fail [OK]
Common Mistakes:
  • Thinking test passes despite long sleep
  • Assuming test fails immediately without waiting
  • Believing test is skipped instead of failed
4. This JUnit 5 test uses @Timeout(1) but does not compile. What is the error?
@Test
@Timeout(1)
void testMethod() {
    // test code
}
medium
A. Missing import for @Timeout annotation
B. Timeout annotation requires a time unit parameter
C. Timeout value must be a string, not an integer
D. Timeout annotation cannot be used on void methods

Solution

  1. Step 1: Check @Timeout annotation parameters

    The @Timeout annotation requires both a value and a time unit parameter in JUnit 5.
  2. Step 2: Identify missing time unit

    Using @Timeout(1) without specifying the time unit causes a compilation error.
  3. Final Answer:

    Timeout annotation requires a time unit parameter -> Option B
  4. Quick Check:

    Missing time unit parameter = A [OK]
Hint: Specify time unit with @Timeout [OK]
Common Mistakes:
  • Forgetting to specify time unit
  • Using @Timeout without time unit
  • Thinking @Timeout only works on non-void methods
5. You want to apply a timeout of 500 milliseconds to all test methods in a JUnit 5 test class. Which is the best way to do this?
hard
A. Add @Timeout(value = 500, unit = TimeUnit.MILLISECONDS) on the test class
B. Use Thread.sleep(500) in each test method
C. Set a global timeout property in the JUnit configuration file
D. Add @Timeout(value = 500, unit = TimeUnit.MILLISECONDS) on each test method

Solution

  1. Step 1: Understand class-level @Timeout usage

    JUnit 5 allows applying @Timeout at the class level to set a timeout for all test methods inside.
  2. Step 2: Compare options for efficiency

    Adding @Timeout on the class is simpler and ensures all tests have the timeout without repeating annotation.
  3. Final Answer:

    Add @Timeout(value = 500, unit = TimeUnit.MILLISECONDS) on the test class -> Option A
  4. Quick Check:

    Class-level @Timeout applies to all tests = D [OK]
Hint: Put @Timeout on class to affect all tests [OK]
Common Mistakes:
  • Adding @Timeout repeatedly on each method
  • Trying to set timeout via sleep calls
  • Assuming global config sets timeout automatically
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