Operations 6 min read

Automate JUnit Tests in Jenkins: Step‑by‑Step Guide for CI/CD

This article explains how to integrate JUnit unit testing into Jenkins pipelines using Maven and Spring Boot, covering project setup, dependency configuration, test case creation, Jenkins job configuration, and result reporting to achieve fully automated testing in a DevOps workflow.

FunTester
FunTester
FunTester
Automate JUnit Tests in Jenkins: Step‑by‑Step Guide for CI/CD

With the rise of DevOps, automating software builds, tests, and deployments has become essential for faster and more reliable releases. Jenkins, an open‑source CI tool, handles the build and deployment stages, but it does not run tests by default. This guide shows how to add JUnit testing to Jenkins so that tests execute automatically during each build.

Project Setup

The author uses Git for version control, Maven as the build tool, and a Spring Boot application. After configuring Jenkins for automatic builds, the next step is to create a Maven project (e.g., via Eclipse’s New Maven Project ) and define the groupId and artifactId for the parent POM.

Once the parent project is created, add a Maven module for the application and write the business logic under src/main/java.

Adding JUnit Dependencies

To enable JUnit testing, include the spring-boot-starter-test dependency in the module’s pom.xml:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-test</artifactId>
</dependency>

This starter brings in JUnit, Mockito, and other testing utilities required for Spring Boot tests.

Creating Test Cases

Place test classes under src/test/java. Maven automatically picks up this directory during the test phase. A typical test class uses annotations such as @RunWith(SpringRunner.class) (or @SpringBootTest for JUnit 5), @Before for setup, and @Test for the test methods. Example annotations: @RunWith: links Spring’s test context with JUnit 4. @SpringBootTest: loads the full application context. @Before: runs preparation code before each test. @Test: marks a method as a test case.

Configuring Jenkins

In the Jenkins job configuration, add the Maven test command (e.g., mvn test) to the build steps. This ensures that after the code is compiled, Jenkins executes the JUnit tests automatically.

Jenkins will display the test results for each build, and you can configure email notifications to send the test summary to the development team.

Result Verification

After the configuration, each code push triggers Jenkins to build the project, run the JUnit tests, and publish the test report. The article includes screenshots of the Jenkins UI showing the test results and the email notification setup.

Conclusion

Integrating JUnit into Jenkins automates unit testing, reduces manual effort, and provides immediate feedback on code quality. Maintaining up‑to‑date test cases is essential to keep the automation reliable.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ci/cdAutomationtestingDevOpsmavenJUnitJenkins
FunTester
Written by

FunTester

10k followers, 1k articles | completely useless

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.