0
0
JUnittesting~15 mins

Maven dependency setup in JUnit - Build an Automation Script

Choose your learning style9 modes available
Verify JUnit dependency setup in Maven project
Preconditions (2)
Step 1: Open the project's pom.xml file
Step 2: Add the JUnit 5 dependency inside the <dependencies> tag
Step 3: Save the pom.xml file
Step 4: Run 'mvn clean test' command in the project directory
Step 5: Observe the build and test execution output
✅ Expected Result: Maven downloads the JUnit 5 dependency, compiles the project, and runs tests successfully without errors
Automation Requirements - JUnit 5 with Maven
Assertions Needed:
Verify that the JUnit 5 dependency is present in the pom.xml
Verify that 'mvn test' command runs successfully
Verify that test classes using JUnit 5 annotations execute and pass
Best Practices:
Use the latest stable JUnit 5 version in dependency
Place dependencies inside <dependencies> tag in pom.xml
Use Maven lifecycle commands for build and test
Keep pom.xml well formatted and valid XML
Automated Solution
JUnit
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
                             http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo-project</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter</artifactId>
            <version>5.10.0</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>3.1.2</version>
                <configuration>
                    <useModulePath>false</useModulePath>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

This pom.xml file sets up a Maven project with the JUnit 5 dependency.

The <dependency> tag inside <dependencies> adds JUnit Jupiter version 5.10.0 for testing.

The scope is set to test so JUnit is only used during test phases.

The maven-surefire-plugin is configured to run tests during the Maven test lifecycle phase.

Running mvn clean test downloads dependencies, compiles code, and runs tests using JUnit 5.

This setup ensures tests annotated with JUnit 5 annotations like @Test will execute correctly.

Common Mistakes - 4 Pitfalls
Using an outdated JUnit version in the dependency
Placing the JUnit dependency outside the <dependencies> tag
{'mistake': "Not setting the scope to 'test' for JUnit dependency", 'why_bad': 'JUnit jars will be included in production builds unnecessarily, increasing size', 'correct_approach': 'Set <scope>test</scope> so JUnit is only used during testing'}
Not configuring the surefire plugin or using an incompatible version
Bonus Challenge

Now add data-driven testing with 3 different inputs using JUnit 5 parameterized tests

Show Hint