Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
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.
✗ Incorrect
The @Timeout annotation takes the timeout duration in milliseconds. Here, 100 means the test must finish within 100 ms.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing @Test instead of @Timeout.
Using annotations from other packages.
✗ Incorrect
The Timeout annotation is imported from org.junit.jupiter.api.Timeout to set time limits on tests.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using ChronoUnit which is for date/time, not Timeout.
Using Duration.SECONDS which does not exist.
✗ Incorrect
The Timeout annotation uses java.util.concurrent.TimeUnit to specify the time unit. Use TimeUnit.SECONDS for seconds.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up value and unit parameters.
Using seconds unit with 500 value expecting milliseconds.
✗ Incorrect
Set value to 500 and unit to TimeUnit.MILLISECONDS to specify a 500 ms timeout.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect unit for timeout.
Forgetting quotes around display name string.
✗ Incorrect
Set value to 3, unit to TimeUnit.SECONDS, and display name to "Test with 3 seconds timeout" for clarity.