Continuous Integration (CI) is a software development practice where developers frequently integrate their code into a shared repository, usually several times a day. Each integration is verified by an automated build and automated tests, allowing teams to detect problems early.
In this section, we will cover how to integrate JUnit tests into a CI pipeline. We will use popular CI tools like Jenkins and GitHub Actions as examples.
Key Concepts
- Continuous Integration (CI): A development practice that requires developers to integrate code into a shared repository frequently.
- Automated Build: A process that compiles the code and runs automated tests to ensure the codebase is stable.
- CI Tools: Software that automates the process of integrating code changes, building the project, and running tests.
Setting Up CI with Jenkins
Step 1: Install Jenkins
- Download Jenkins from the official website.
- Follow the installation instructions for your operating system.
Step 2: Create a New Jenkins Job
- Open Jenkins in your web browser.
- Click on "New Item" to create a new job.
- Enter a name for your job and select "Freestyle project."
- Click "OK."
Step 3: Configure the Job
-
Source Code Management:
- Select "Git" and enter the repository URL.
- Provide credentials if necessary.
-
Build Triggers:
- Select "Poll SCM" and set the schedule (e.g.,
H/5 * * * *
to poll every 5 minutes).
- Select "Poll SCM" and set the schedule (e.g.,
-
Build Environment:
- Add any necessary environment variables.
-
Build Steps:
- Add a build step to execute a shell command or a Windows batch command.
- For a Maven project, you can use:
mvn clean test
Step 4: Save and Build
- Click "Save" to save the job configuration.
- Click "Build Now" to trigger the build manually.
- Check the build history and console output to ensure the build and tests run successfully.
Setting Up CI with GitHub Actions
Step 1: Create a GitHub Repository
- Create a new repository on GitHub or use an existing one.
- Ensure your project contains a
pom.xml
file if you are using Maven.
Step 2: Add a GitHub Actions Workflow
- In your repository, navigate to the "Actions" tab.
- Click on "New workflow" and select "Set up a workflow yourself."
- Add the following YAML configuration to create a workflow that runs JUnit tests:
name: Java CI with Maven on: push: branches: [ main ] pull_request: branches: [ main ] jobs: build: runs-on: ubuntu-latest steps: - name: Checkout code uses: actions/checkout@v2 - name: Set up JDK 11 uses: actions/setup-java@v1 with: java-version: 11 - name: Build with Maven run: mvn clean install - name: Run tests run: mvn test
Step 3: Commit and Push
- Commit the workflow file to your repository.
- Push the changes to GitHub.
- Navigate to the "Actions" tab to see the workflow running.
Practical Exercise
Exercise: Integrate JUnit Tests with Jenkins
- Objective: Set up a Jenkins job to run JUnit tests for a sample Maven project.
- Steps:
- Install Jenkins.
- Create a new Jenkins job.
- Configure the job to pull code from a Git repository.
- Add a build step to run
mvn clean test
. - Trigger the build and verify the tests run successfully.
Solution:
-
Install Jenkins:
- Follow the installation instructions from the Jenkins website.
-
Create a New Jenkins Job:
- Open Jenkins and click "New Item."
- Enter a name and select "Freestyle project."
- Click "OK."
-
Configure the Job:
- Under "Source Code Management," select "Git" and enter the repository URL.
- Under "Build Triggers," select "Poll SCM" and set the schedule.
- Under "Build Steps," add a shell command:
mvn clean test
-
Save and Build:
- Click "Save."
- Click "Build Now."
- Check the console output to ensure the tests run successfully.
Summary
In this section, we learned how to integrate JUnit tests into a CI pipeline using Jenkins and GitHub Actions. We covered the steps to set up Jenkins, create a new job, configure the job to run JUnit tests, and verify the build. We also explored how to set up a GitHub Actions workflow to run JUnit tests on code pushes and pull requests.
By integrating JUnit tests into your CI pipeline, you can ensure that your codebase remains stable and that any issues are detected early in the development process. This practice is essential for maintaining high-quality software and enabling rapid development cycles.
JUnit Course
Module 1: Introduction to JUnit
Module 2: Basic JUnit Annotations
- Understanding @Test
- Using @Before and @After
- Using @BeforeClass and @AfterClass
- Ignoring Tests with @Ignore
Module 3: Assertions in JUnit
Module 4: Parameterized Tests
- Introduction to Parameterized Tests
- Creating Parameterized Tests
- Using @ParameterizedTest
- Custom Parameterized Tests
Module 5: Test Suites
Module 6: Mocking with JUnit
Module 7: Advanced JUnit Features
Module 8: Best Practices and Tips
- Writing Effective Tests
- Organizing Test Code
- Test-Driven Development (TDD)
- Continuous Integration with JUnit