0
0
JUnittesting~10 mins

Timeout annotations in JUnit - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set a timeout of 100 milliseconds on the test method.

JUnit
@Test
@Timeout([1])
void testQuickOperation() {
    // test code here
}
Drag options to blanks, or click blank then click option'
A10
B1000
C100
D500
Attempts:
3 left
💡 Hint
Common Mistakes
Using 1000 instead of 100 causes longer timeout than intended.
Omitting the @Timeout annotation means no timeout is set.
2fill in blank
medium

Complete the code to import the Timeout annotation from JUnit Jupiter.

JUnit
import org.junit.jupiter.api.[1];

public class MyTests {
    @Test
    @Timeout(200)
    void testMethod() {}
}
Drag options to blanks, or click blank then click option'
ATest
BAfterAll
CBeforeEach
DTimeout
Attempts:
3 left
💡 Hint
Common Mistakes
Importing @Test instead of @Timeout.
Using annotations from other packages.
3fill in blank
hard

Fix the error in the timeout annotation usage to specify the time unit as seconds.

JUnit
@Test
@Timeout(value = 2, unit = [1])
void testWithSecondsTimeout() {
    // test code
}
Drag options to blanks, or click blank then click option'
AChronoUnit.SECONDS
BTimeUnit.SECONDS
CTimeUnit.MILLISECONDS
DDuration.SECONDS
Attempts:
3 left
💡 Hint
Common Mistakes
Using ChronoUnit which is for date/time, not Timeout.
Using Duration.SECONDS which does not exist.
4fill in blank
hard

Fill both blanks to set a timeout of 500 milliseconds using the Timeout annotation with explicit value and unit.

JUnit
@Test
@Timeout(value = [1], unit = [2])
void testHalfSecondTimeout() {
    // test code
}
Drag options to blanks, or click blank then click option'
A500
B1000
CTimeUnit.MILLISECONDS
DTimeUnit.SECONDS
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up value and unit parameters.
Using seconds unit with 500 value expecting milliseconds.
5fill in blank
hard

Fill all three blanks to create a test method with a 3-second timeout and a descriptive display name.

JUnit
@Test
@Timeout(value = [1], unit = [2])
@DisplayName([3])
void testTimeoutWithName() {
    // test code
}
Drag options to blanks, or click blank then click option'
A3
BTimeUnit.SECONDS
C"Test with 3 seconds timeout"
D"Timeout test"
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect unit for timeout.
Forgetting quotes around display name string.