C. assertTimeout requires a timeout in milliseconds
D. Missing lambda expression to defer execution
Solution
Step 1: Check how code block is passed
assertTimeout expects a lambda or executable to delay running until assertion.
Step 2: Identify missing lambda
Here, processData() is called immediately, not deferred by lambda.
Final Answer:
Missing lambda expression to defer execution -> Option D
Quick Check:
assertTimeout needs lambda to delay execution = A [OK]
Hint: Always wrap code in lambda for assertTimeout [OK]
Common Mistakes:
Calling method directly instead of lambda
Using wrong Duration method
Expecting assertTimeout to accept milliseconds as int
5. You want to test a method calculate() that sometimes takes longer but should not exceed 3 seconds. Which approach correctly uses assertTimeout to fail the test if calculate() exceeds 3 seconds, and also returns the method's result for further checks?
hard
A. var result = assertTimeout(Duration.ofSeconds(3), () -> { return calculate(); });
B. assertTimeout(Duration.ofSeconds(3), () -> { calculate(); });
C. assertTimeout(Duration.ofSeconds(3), () -> { return calculate(); });
D. var result = assertTimeout(Duration.ofSeconds(3), calculate());
Solution
Step 1: Understand assertTimeout return value
assertTimeout returns the value from the lambda if it completes in time.
Step 2: Use lambda with return and assign result
To capture calculate() result, lambda must return it and assign to a variable.
Step 3: Check syntax correctness
var result = assertTimeout(Duration.ofSeconds(3), () -> { return calculate(); }); uses lambda with return and assigns to result, matching requirements.
Final Answer:
var result = assertTimeout(Duration.ofSeconds(3), () -> { return calculate(); }); -> Option A
Quick Check:
Return value + assign result + timeout = D [OK]
Hint: Use lambda with return and assign to capture result [OK]
Common Mistakes:
Not returning value inside lambda
Not assigning assertTimeout result
Using lambda without return for non-void methods
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.