Bird
Raised Fist0
JUnittesting~20 mins

Maven Surefire plugin in JUnit - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Surefire Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Surefire Plugin Default Test Inclusion

Given the following Maven Surefire plugin configuration snippet, which test classes will be executed by default?

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M7</version>
</plugin>
AAll classes in the src/main/java directory
BOnly classes named exactly Test.java
COnly classes annotated with @RunWith
DAll classes matching **/Test*.java, **/*Test.java, and **/*Tests.java
Attempts:
2 left
💡 Hint

Think about the default naming patterns Surefire uses to find test classes.

❓ assertion
intermediate
2:00remaining
Surefire Plugin Failsafe vs Surefire

Which assertion about the Maven Surefire and Failsafe plugins is correct?

ASurefire runs unit tests during the test phase; Failsafe runs integration tests during the integration-test phase
BFailsafe runs unit tests during the test phase; Surefire runs integration tests during the integration-test phase
CBoth Surefire and Failsafe run tests only during the package phase
DSurefire runs tests only if the project uses JUnit 5
Attempts:
2 left
💡 Hint

Consider the Maven lifecycle phases and typical plugin usage.

🔧 Debug
advanced
2:00remaining
Debugging Surefire Test Skipping

A developer notices that no tests run when executing mvn test even though test classes exist. The Surefire plugin is configured as below:

<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M7</version>
  <configuration>
    <skipTests>true</skipTests>
  </configuration>
</plugin>

What is the reason tests are not running?

AThe <code>skipTests</code> parameter is set to true, so Surefire skips all tests
BTests are skipped because the <code>test</code> phase is not bound to Surefire by default
CThe plugin version 3.0.0-M7 does not support running tests
DTests are skipped because the test classes are not in the default directory
Attempts:
2 left
💡 Hint

Check the plugin configuration parameters related to skipping tests.

❓ framework
advanced
2:00remaining
Surefire Plugin and JUnit 5 Support

Which configuration is required to enable JUnit 5 tests to run with Maven Surefire plugin version 3.0.0-M7?

ANo additional configuration is needed; Surefire 3.0.0-M7 supports JUnit 5 by default
BAdd <code>&lt;dependency&gt;org.junit.jupiter:junit-jupiter-engine&lt;/dependency&gt;</code> and configure Surefire to use <code>junitPlatform</code> provider
CUse the Failsafe plugin instead of Surefire for JUnit 5 tests
DAdd <code>&lt;dependency&gt;junit:junit&lt;/dependency&gt;</code> to support JUnit 5
Attempts:
2 left
💡 Hint

Think about the provider and dependencies needed for JUnit 5.

🧠 Conceptual
expert
2:00remaining
Surefire Plugin Parallel Test Execution

Which statement about configuring parallel test execution in Maven Surefire plugin is correct?

ASetting <code>parallel</code> to <code>classes</code> runs test classes sequentially
BParallel execution is not supported by Surefire plugin
CSetting <code>parallel</code> to <code>methods</code> runs test methods in parallel within the same class
DSurefire requires external tools to run tests in parallel
Attempts:
2 left
💡 Hint

Consider the parallel modes Surefire supports.

Practice

(1/5)
1. What is the main purpose of the Maven Surefire plugin in a JUnit testing project?
easy
A. To deploy the application to a server
B. To compile Java source code
C. To run tests automatically during the build process
D. To package the application into a JAR file

Solution

  1. Step 1: Understand the role of Maven Surefire plugin

    The Maven Surefire plugin is designed to run tests automatically during the build process, ensuring code correctness early.
  2. Step 2: Differentiate from other Maven plugins

    Other plugins like compiler or deploy handle compiling or deployment, not running tests.
  3. Final Answer:

    To run tests automatically during the build process -> Option C
  4. Quick Check:

    Maven Surefire runs tests during build [OK]
Hint: Surefire runs tests during build, not compile or deploy [OK]
Common Mistakes:
  • Confusing Surefire with compiler plugin
  • Thinking Surefire packages the app
  • Assuming Surefire deploys the app
2. Which XML tag correctly configures the Maven Surefire plugin in the pom.xml file?
easy
A. org.apache.maven.pluginsmaven-jar-plugin
B. org.apache.maven.pluginsmaven-compiler-plugin
C. org.junit.jupiterjunit-jupiter-engine
D. org.apache.maven.pluginsmaven-surefire-plugin

Solution

  1. Step 1: Identify the correct plugin artifactId for Surefire

    The Maven Surefire plugin uses maven-surefire-plugin as its artifactId.
  2. Step 2: Confirm groupId and artifactId match Surefire plugin

    The groupId is org.apache.maven.plugins, which is standard for Maven plugins.
  3. Final Answer:

    <plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId></plugin> -> Option D
  4. Quick Check:

    Surefire plugin artifactId = maven-surefire-plugin [OK]
Hint: Surefire plugin artifactId always contains 'surefire' [OK]
Common Mistakes:
  • Using maven-compiler-plugin instead of surefire
  • Confusing JUnit engine with Surefire plugin
  • Using maven-jar-plugin for testing
3. Given this pom.xml snippet, what will happen when you run mvn test?
<plugin>
  <groupId>org.apache.maven.plugins</groupId>
  <artifactId>maven-surefire-plugin</artifactId>
  <version>3.0.0-M7</version>
  <configuration>
    <includes>
      <include>**/*Test.java</include>
    </includes>
  </configuration>
</plugin>
medium
A. Only Java files ending with 'Test.java' will be run as tests
B. All Java files will be compiled but no tests will run
C. Tests will run only if file names start with 'Test'
D. No tests will run because version is invalid

Solution

  1. Step 1: Understand the includes configuration

    The <includes> tag specifies which test files to run. Here, it includes files matching **/*Test.java.
  2. Step 2: Interpret the pattern meaning

    The pattern means any Java file ending with 'Test.java' in any folder will be run as a test.
  3. Final Answer:

    Only Java files ending with 'Test.java' will be run as tests -> Option A
  4. Quick Check:

    Includes pattern '**/*Test.java' runs matching test files [OK]
Hint: Includes '**/*Test.java' runs files ending with 'Test.java' [OK]
Common Mistakes:
  • Assuming all Java files run as tests
  • Thinking files starting with 'Test' run
  • Believing version 3.0.0-M7 is invalid
4. You added the Maven Surefire plugin to your pom.xml but tests are not running. Which of these is the most likely cause?
medium
A. The pom.xml file is not saved
B. The artifactId is misspelled as maven-surefire-plugn
C. The version tag is missing
D. JUnit tests are written in a different folder than src/test/java

Solution

  1. Step 1: Check plugin artifactId spelling

    If the artifactId is misspelled, Maven cannot find the plugin, so tests won't run.
  2. Step 2: Consider other causes

    Missing version usually defaults to latest, unsaved pom.xml is unlikely, and test folder location matters but less common cause.
  3. Final Answer:

    The artifactId is misspelled as maven-surefire-plugn -> Option B
  4. Quick Check:

    Misspelled artifactId stops Surefire plugin loading [OK]
Hint: Check artifactId spelling first if tests don't run [OK]
Common Mistakes:
  • Ignoring typo in artifactId
  • Assuming missing version always breaks build
  • Not verifying test source folder
5. You want to run only tests tagged with @Fast annotation using Maven Surefire plugin. Which configuration snippet in pom.xml achieves this?
hard
A. Fast
B. Fast
C. **/*Fast.java
D. Fast*

Solution

  1. Step 1: Understand Surefire's group filtering

    Surefire uses <groups> to include tests with specific JUnit categories or tags.
  2. Step 2: Match the correct tag for including groups

    Using <groups>Fast</groups> runs only tests tagged with @Fast.
  3. Final Answer:

    <configuration><groups>Fast</groups></configuration> -> Option A
  4. Quick Check:

    Use <groups> to run tagged tests [OK]
Hint: Use <groups> tag to run tests with specific annotations [OK]
Common Mistakes:
  • Using excludedGroups instead of groups
  • Filtering by filename instead of annotation
  • Using <test> tag incorrectly
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