0
0
JUnittesting~8 mins

Maven dependency setup in JUnit - Framework Patterns

Choose your learning style9 modes available
Framework Mode - Maven dependency setup
Folder Structure
my-test-project/
├── src/
│   ├── main/
│   │   └── java/               # Application source code
│   └── test/
│       └── java/               # Test source code
│           └── com/example/tests/
│               └── ExampleTest.java
├── pom.xml                    # Maven project file with dependencies
└── README.md
  
Test Framework Layers
  • Dependency Management: Managed in pom.xml to include JUnit and other libraries.
  • Test Source Code: Located under src/test/java, contains JUnit test classes.
  • Application Code: Under src/main/java, separate from tests.
  • Build and Run: Maven handles compiling, running tests, and managing dependencies.
Configuration Patterns

The pom.xml file manages dependencies and plugins. Example snippet for JUnit 5:

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

<build>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-surefire-plugin</artifactId>
      <version>3.0.0</version>
    </plugin>
  </plugins>
</build>
  

This setup ensures tests run with JUnit 5 and Maven's Surefire plugin during mvn test.

Test Reporting and CI/CD Integration
  • Maven Surefire plugin generates test reports in target/surefire-reports.
  • Reports include XML and plain text summaries for CI tools.
  • CI/CD pipelines (e.g., Jenkins, GitHub Actions) run mvn test to execute tests and collect reports.
  • Failing tests cause build failures, alerting developers early.
Best Practices
  1. Keep test dependencies scoped as test to avoid bloating production builds.
  2. Use explicit versions for dependencies to ensure consistent builds.
  3. Separate test code from application code in src/test/java and src/main/java.
  4. Use Maven plugins like Surefire for running tests and generating reports.
  5. Keep pom.xml clean and organized for easy maintenance.
Self Check

Where in this folder structure would you add a new JUnit test class for a feature called "Login"?

Key Result
Use Maven's pom.xml to manage JUnit dependencies and plugins for clean, maintainable test automation.