Bird
Raised Fist0
JUnittesting~20 mins

Parallel test configuration in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Parallel Test Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
JUnit Parallel Execution Output
Given the following JUnit 5 test class configured to run tests in parallel, what will be the output order of the test methods?
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 ParallelTest {

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

    @Test
    void testB() {
        System.out.println("B");
    }

    @Test
    void testC() throws InterruptedException {
        Thread.sleep(50);
        System.out.println("C");
    }
}
AB, C, A
BA, B, C
CC, B, A
DB, A, C
Attempts:
2 left
💡 Hint
Consider that tests run concurrently and sleep delays affect output timing.
❓ assertion
intermediate
1:30remaining
Correct Assertion for Parallel Test Count
In a JUnit 5 test suite configured with junit.jupiter.execution.parallel.enabled = true and junit.jupiter.execution.parallel.config.fixed.parallelism = 3, which assertion correctly verifies that exactly 3 tests run in parallel?
AassertTrue(parallelismLevel >= 3);
BassertNotNull(parallelismLevel);
CassertFalse(parallelismLevel > 3);
DassertEquals(3, parallelismLevel);
Attempts:
2 left
💡 Hint
The fixed parallelism config sets the exact number of parallel threads.
🔧 Debug
advanced
2:00remaining
Debugging Parallel Test Failures
A JUnit 5 test suite runs tests in parallel but intermittently fails due to shared resource conflicts. Which change best fixes this issue?
ARemove <code>@Execution(ExecutionMode.CONCURRENT)</code> to run tests sequentially.
BIncrease <code>fixed.parallelism</code> to reduce test overlap.
CAdd <code>@ResourceLock("sharedResource")</code> to tests accessing the resource.
DUse <code>@TestInstance(Lifecycle.PER_CLASS)</code> to share state across tests.
Attempts:
2 left
💡 Hint
JUnit 5 provides annotations to control resource locking in parallel tests.
❓ framework
advanced
1:30remaining
JUnit 5 Parallel Execution Configuration
Which configuration snippet correctly enables parallel test execution with a fixed thread pool of 4 threads in JUnit 5?
A
junit.jupiter.execution.parallel.enabled = false
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism = 4
B
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism = 4
C
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.config.strategy = dynamic
junit.jupiter.execution.parallel.config.fixed.parallelism = 4
D
junit.jupiter.execution.parallel.enabled = true
junit.jupiter.execution.parallel.config.strategy = fixed
junit.jupiter.execution.parallel.config.fixed.parallelism = 2
Attempts:
2 left
💡 Hint
Parallel execution must be enabled and strategy set to fixed with desired parallelism.
🧠 Conceptual
expert
2:30remaining
Impact of Parallel Test Execution on Test Isolation
Which statement best describes the impact of enabling parallel test execution in JUnit 5 on test isolation?
AParallel execution can cause shared mutable state issues unless tests are designed for isolation.
BParallel execution guarantees complete test isolation by default.
CJUnit 5 disables static variables when parallel execution is enabled to ensure isolation.
DTests running in parallel always share the same instance, so isolation is not possible.
Attempts:
2 left
💡 Hint
Think about how shared data affects tests running at the same time.

Practice

(1/5)
1. What is the main purpose of configuring parallel test execution in JUnit?
easy
A. To disable all tests temporarily
B. To run tests one after another in a fixed order
C. To run multiple tests at the same time and save testing time
D. To automatically fix failing tests

Solution

  1. Step 1: Understand parallel test execution

    Parallel test execution means running tests simultaneously instead of one by one.
  2. Step 2: Identify the benefit

    Running tests at the same time saves overall testing time.
  3. Final Answer:

    To run multiple tests at the same time and save testing time -> Option C
  4. Quick Check:

    Parallel tests = faster testing [OK]
Hint: Parallel tests run simultaneously to save time [OK]
Common Mistakes:
  • Thinking parallel means disabling tests
  • Confusing parallel with sequential execution
  • Assuming parallel fixes test failures
2. Which annotation enables parallel execution of tests in JUnit 5?
easy
A. @Execution(ExecutionMode.CONCURRENT)
B. @RunWith(ParallelRunner.class)
C. @ParallelTest
D. @ConcurrentExecution

Solution

  1. Step 1: Recall JUnit 5 parallel test annotation

    JUnit 5 uses @Execution with ExecutionMode.CONCURRENT to run tests in parallel.
  2. Step 2: Check other options

    @RunWith is from JUnit 4, and @ParallelTest or @ConcurrentExecution do not exist in JUnit 5.
  3. Final Answer:

    @Execution(ExecutionMode.CONCURRENT) -> Option A
  4. Quick Check:

    @Execution(CONCURRENT) enables parallel tests [OK]
Hint: Use @Execution(ExecutionMode.CONCURRENT) for parallel tests [OK]
Common Mistakes:
  • Using JUnit 4 annotations in JUnit 5
  • Assuming @ParallelTest exists
  • Confusing annotation names
3. Given the following JUnit 5 test class, what will be the output when tests run with @Execution(ExecutionMode.CONCURRENT)?
import org.junit.jupiter.api.*;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;

@TestInstance(TestInstance.Lifecycle.PER_CLASS)
@Execution(ExecutionMode.CONCURRENT)
public class SampleTest {

    @Test
    void testA() throws InterruptedException {
        Thread.sleep(100);
        System.out.println("Test A completed");
    }

    @Test
    void testB() {
        System.out.println("Test B completed");
    }
}
medium
A. Test A completed Test B completed (always in this order)
B. Test B completed Test A completed (order may vary due to concurrency)
C. Compilation error due to @Execution usage
D. Tests run sequentially ignoring @Execution

Solution

  1. Step 1: Understand @Execution(ExecutionMode.CONCURRENT)

    This annotation runs tests in parallel, so testA and testB start at the same time.
  2. Step 2: Analyze test behavior

    testA sleeps 100ms before printing, testB prints immediately. So testB likely prints first, but order can vary.
  3. Final Answer:

    Test B completed Test A completed (order may vary due to concurrency) -> Option B
  4. Quick Check:

    Parallel tests print order varies [OK]
Hint: Sleep delays show concurrency by changing print order [OK]
Common Mistakes:
  • Assuming tests run sequentially despite @Execution
  • Expecting fixed print order
  • Thinking @Execution causes compile errors
4. You configured @Execution(ExecutionMode.CONCURRENT) but tests fail intermittently due to shared data. What is the best fix?
medium
A. Make tests independent by avoiding shared mutable state
B. Remove @Execution and run tests sequentially
C. Add Thread.sleep() to all tests
D. Use @Disabled on all tests

Solution

  1. Step 1: Identify cause of intermittent failures

    Shared mutable data causes race conditions when tests run in parallel.
  2. Step 2: Apply best practice for parallel tests

    Tests must be independent and avoid shared mutable state to run safely in parallel.
  3. Final Answer:

    Make tests independent by avoiding shared mutable state -> Option A
  4. Quick Check:

    Independent tests avoid parallel conflicts [OK]
Hint: Avoid shared data for stable parallel tests [OK]
Common Mistakes:
  • Just removing parallel execution instead of fixing tests
  • Adding sleeps does not fix race conditions
  • Disabling tests hides the problem
5. You want to run tests in parallel but only within each test class, not across classes. Which configuration achieves this in JUnit 5?
hard
A. Annotate test classes with @Execution(ExecutionMode.SAME_THREAD)
B. Use @Execution(ExecutionMode.CONCURRENT) on each test method only
C. Set junit.jupiter.execution.parallel.enabled to false
D. Set junit.jupiter.execution.parallel.mode.default to concurrent and junit.jupiter.execution.parallel.mode.classes.default to same_thread

Solution

  1. Step 1: Understand JUnit 5 parallel config properties

    Property junit.jupiter.execution.parallel.mode.default controls method-level parallelism; junit.jupiter.execution.parallel.mode.classes.default controls class-level parallelism.
  2. Step 2: Configure methods parallel, classes sequential

    Setting methods to concurrent and classes to same_thread runs tests in parallel within classes but not across classes.
  3. Final Answer:

    Set junit.jupiter.execution.parallel.mode.default to concurrent and junit.jupiter.execution.parallel.mode.classes.default to same_thread -> Option D
  4. Quick Check:

    Methods parallel + classes sequential = parallel in class only [OK]
Hint: Configure method and class parallel modes separately [OK]
Common Mistakes:
  • Assuming @Execution on methods controls class-level parallelism
  • Disabling parallel execution entirely
  • Using SAME_THREAD on classes disables all parallelism
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard