Running Tests in GitLab CI: From Zero to Pipeline



This content originally appeared on DEV Community and was authored by Thiago Matos

Continuous Integration (CI) are essential practices in modern software development. They ensure that code changes are automatically tested, leading to faster and more reliable software releases. In this article, we’ll walk through the process of setting up a Spring Boot project with Gradle, writing unit tests, and configuring GitLab CI to automate the testing.

Step 1: Install SDKMAN and Java 21

First, we need to install SDKMAN, a tool for managing parallel versions of multiple Software Development Kits (SDKs), including Java.

curl -s "https://get.sdkman.io" | bash
source "$HOME/.sdkman/bin/sdkman-init.sh"

You can close and open again your terminal to assure your installation has gotten success.

Second, use sdkman to install Java 21, the most stable version.
The command bellow shows the available versions:

sdk list java

List of available Java versions
You can choose one of your preference. Use the Identifier column as reference to install the chosen version:

sdk install java 21.0.2-open

Third, confirm you’ve installed successfully

java --version

Step 2: Install Git

Next, install Git, a version control system that we’ll use to manage code changes of our project.

sudo apt-get update
sudo apt-get install git

Step 3 Install IntelliJ IDEA

IntelliJ IDEA is a popular IDE(Integrated Development Environment) for Java development. Here is the command to install it:

sudo snap install intellij-idea-community --classic

Step 4: Create a Spring Boot Project

We’ll use Spring Initializr to create a new project for the most recent version.
Acess https://start.spring.io/ and configure the project as follows:

  1. Project: Gradle – Kotlin
  2. Language: Java
  3. Spring Boot: 3.3.1
  4. Packaging: jar
  5. Java: 21
  6. Dependencies: No dependency is necessary

Click on the button GENERATE to download the project as zip.

Spring Initializr Configured

Step 5: Import the Project into IntelliJ

Unzip the downloaded file into a folder of your preference.
Open IntelliJ IDEA and import the unzipped project. IntelliJ will automatically detect the Gradle configuration and set up the project accordingly.

Step 6: Make a TDD

First, create the Unit Test.
Create a simple class and a corresponding unit test.
Create a new class Calculator in src/main/java/com/example/demo:

public class Calculator {
    public int add(int a, int b) {
        return 0;
    }
}

Now, create a unit test for this class in src/test/java/com/example/demo:

import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
class CalculatorTest {
    @Test
    void testAdd() {
        var calculator = new Calculator();
        var result = calculator.add(2, 3);
        assertThat(result).isEqualTo(5);
    }
}

Second, let the test fail.
Press Green Button to run the test.
Press Green Button to run the test

It’ll fail.

Test failing

Third, fix the test by writing the correct bussiness rule:

public class Calculator {
    public int add(int a, int b) {
        return a + b;
    }
}

And, at last, run the test again and see that now it is passing.

Test passing

Step 6: Create a Gitlab CI Configuration File

Create a .gitlab-ci.yml file in the root of your project. This file will define the pipeline.

stages:
  - build

Build:
  stage: build
  image: gradle:8.8.0-alpine
  script:
    - gradle --build-cache clean build
  artifacts:
    when: always
    expire_in: 1 days
    paths:
      - build/libs/*.jar
      - build/test-results/test/*.xml
    reports:
      junit:
        - build/test-results/test/*.xml
  only:
    - main

stages: define a sequence of steps that the pipeline will execute. The pipeline runs stages in the order they are defined, and all jobs within the same stage run in parallel. Only after all jobs in a stage complete successfully does the pipeline proceed to the next stage.
Build: name of the job to be executed for the stage.
Image: Docker Image that will be used to execute the job.
script: gradle command to generate the jar, run the tests and generate a report about the tests.
artifacts: Files that will survive when the job is done.
only: name of the git branch

Step 8: Create a Project on GitLab

Gitlab is a web-based DevOps lifecycle tool that provides a Git repository manager offering source code management, continuous integration, and continouos deployment capabilities.

Go to GitLab and create a new blank project and make it public or private based on your preference.
https://gitlab.com/projects/new#blank_project

New empty gitlab project

Step 9: Configure Git in Project

Open the terminal, go to the directory where you unzipped your code and configure git on it.

git init --initial-branch=main
git remote add origin https://gitlab.com/your-username/spring-boot-unit-test-ci.git
git add .
git commit -m "Initial commit"
git push --set-upstream origin main

Step 10: Check the pipeline running

Go to https://gitlab.com/your-username/spring-boot-unit-test-ci/-/pipelines and click on the first (and only existent) pipeline running.

the first and only existent pipeline

Here you can see the pipeline running:
The pipeline running

After some time, the pipeline passes:
The pipeline passed

Here you can see the test report:
Image description

Conclusion

By following theses steps, you’ve successfully set up a Spring Boot project with Gradle, written unit test, and configured GitLab CI to automate the testing. You can now make a test fail intentionally and observe the pipeline breaking, then fix the test and see the pipeline pass again. This iterative process helps ensure that your code is always in a deployable state.

https://gitlab.com/thiagoematos/spring-boot-unit-test-ci


This content originally appeared on DEV Community and was authored by Thiago Matos