0
0
JUnittesting~20 mins

@Execution annotation in JUnit - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
JUnit Parallel Execution Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Purpose of @Execution annotation in JUnit
What is the primary purpose of the @Execution annotation in JUnit 5?
ATo provide a timeout value for test execution
BTo define the order in which test methods are executed
CTo mark a test method as disabled temporarily
DTo specify whether tests in a class or method should run concurrently or sequentially
Attempts:
2 left
💡 Hint
Think about controlling how tests run in parallel or not.
Predict Output
intermediate
2:00remaining
Output of test execution mode with @Execution
Given the following JUnit 5 test class, what is the expected execution mode of the tests?
JUnit
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@Execution(ExecutionMode.CONCURRENT)
public class SampleTest {

    @Test
    void testOne() throws InterruptedException {
        Thread.sleep(100);
        System.out.println("Test One");
    }

    @Test
    void testTwo() throws InterruptedException {
        Thread.sleep(100);
        System.out.println("Test Two");
    }
}
ATests run concurrently, possibly overlapping in time
BTests run in random order but sequentially
CTests run sequentially, one after another
DTests are skipped due to incorrect annotation usage
Attempts:
2 left
💡 Hint
Look at the ExecutionMode value used in the annotation.
assertion
advanced
2:00remaining
Correct assertion for verifying parallel execution
Which assertion best verifies that two test methods annotated with @Execution(ExecutionMode.CONCURRENT) actually ran in parallel?
AassertEquals(testOneStartTime, testTwoStartTime) // tests started at exactly the same time
BassertTrue(testOneEndTime - testTwoStartTime < 0) // testOne ended before testTwo started
CassertTrue(Math.abs(testOneEndTime - testTwoEndTime) < expectedOverlap) // tests overlapped in time
DassertFalse(testOneEndTime > testTwoStartTime) // testOne ended after testTwo started
Attempts:
2 left
💡 Hint
Parallel tests overlap in execution time, so their end times should be close.
🔧 Debug
advanced
2:00remaining
Why does @Execution annotation have no effect?
A developer added @Execution(ExecutionMode.CONCURRENT) to a test class, but tests still run sequentially. What is the most likely reason?
AThe annotation is misspelled and ignored by JUnit
BThe test runner does not support parallel execution or it is not enabled
CThe tests have @Disabled annotation which forces sequential execution
DJUnit 5 does not support the @Execution annotation
Attempts:
2 left
💡 Hint
Check if parallel execution is enabled in the test environment.
framework
expert
2:00remaining
Combining @Execution with other parallel annotations
In JUnit 5, if a test class is annotated with @Execution(ExecutionMode.CONCURRENT) and individual test methods are annotated with @Execution(ExecutionMode.SAME_THREAD), what is the effective execution mode for those methods?
AMethod-level SAME_THREAD overrides class-level CONCURRENT; those methods run sequentially
BMethods annotated with SAME_THREAD run sequentially; others run concurrently
CClass-level CONCURRENT overrides method-level SAME_THREAD; all run concurrently
DJUnit throws a runtime error due to conflicting annotations
Attempts:
2 left
💡 Hint
Method-level annotations override class-level annotations in JUnit.