Verify JUnit dependency setup in Maven project
Preconditions (2)
✅ Expected Result: Maven downloads the JUnit 5 dependency, compiles the project, and runs tests successfully without errors
<?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.
Now add data-driven testing with 3 different inputs using JUnit 5 parameterized tests
pom.xml file of a Maven project?pom.xml with scope set to test?org.junit.jupiter and junit-jupiter.test to limit usage to testing phase.pom.xml snippet, what will happen when you run mvn test?
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.9.2</version> <scope>test</scope> </dependency>
pom.xml but your tests fail with ClassNotFoundException for JUnit classes. What is the likely problem?
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.9.2</version> </dependency>
junit-jupiter which bundles API and engine.pom.xml?junit-jupiter artifact includes API and engine for tests.scope to test ensures dependency is used only during testing.Pass the test and this pattern moves unknown