0
0
JUnittesting~10 mins

assertTimeout for performance 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 assert that the method finishes within 1 second.

JUnit
assertTimeout(Duration.ofSeconds([1]), () -> methodToTest());
Drag options to blanks, or click blank then click option'
A10
B1
C0
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a timeout value too large or zero, which defeats the purpose of performance testing.
2fill in blank
medium

Complete the code to assert that the lambda expression finishes within 500 milliseconds.

JUnit
assertTimeout(Duration.ofMillis([1]), () -> someFunction());
Drag options to blanks, or click blank then click option'
A1000
B50
C500
D1500
Attempts:
3 left
💡 Hint
Common Mistakes
Confusing milliseconds with seconds and using Duration.ofSeconds() instead.
3fill in blank
hard

Fix the error in the code by completing the blank with the correct import for Duration.

JUnit
import [1];

assertTimeout(Duration.ofSeconds(2), () -> processData());
Drag options to blanks, or click blank then click option'
Ajava.time.Duration
Bjava.io.Duration
Cjava.lang.Duration
Djava.util.Duration
Attempts:
3 left
💡 Hint
Common Mistakes
Importing from the wrong package causes compilation errors.
4fill in blank
hard

Fill both blanks to assert that the method finishes within 3 seconds and returns the expected value.

JUnit
String result = assertTimeout(Duration.ofSeconds([1]), () -> [2]);
assertEquals("success", result);
Drag options to blanks, or click blank then click option'
A3
BperformTask()
Cexecute()
D5
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong method name or wrong timeout duration.
5fill in blank
hard

Fill all three blanks to assert that the method finishes within 200 milliseconds, returns a non-null value, and the result equals "done".

JUnit
var result = assertTimeout(Duration.ofMillis([1]), () -> [2]);
assertNotNull(result);
assertEquals([3], result);
Drag options to blanks, or click blank then click option'
A200
BrunProcess()
C"done"
D"completed"
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong timeout units or wrong expected string.