6. Application Testing

In this lab we will edit our pipeline so that the Java application will be tested automatically every time the pipeline runs. Our Java example application contains a set of JUnit tests, which will be executed during a run. You can think of it as a quality-gate that the pipeline must go through, to pass successfully. When tests fail, due to a change, the pipeline also fails.

Task 6.1: Create test stage

Basic application tests (unit tests) are executed inside a test stage.

Create a new job inside .gitlab-ci.yml with the following configuration:

  • job name: test_application
  • stage: test
  • image: registry.access.redhat.com/ubi9/openjdk-17:latest
  • script:./gradlew check
test job solution

This is how the new job test_application is defined in the .gitlab-ci.yml.

test_application:
  stage: test
  image: registry.access.redhat.com/ubi9/openjdk-17:latest
  script:
    - ./gradlew check

Don’t forget to add the test stage to the stages list to make sure the job will be executed.

stages:
  - info
  - build
  - test

Task 6.2: Check the pipeline

Go to your GitLab project and check the pipelines under Build –> Pipelines.

Check that the test job was successful.

Do you see the executed tests and the results?

When you see them, tell us please. We only have the return value of the test script to decide if all tests run through or one or more failed. Check the next task to see how we capture the test reports.

Task 6.3: Show test reports

The job can be extended with an artifacts configuration. Devs know that the unit test reports are stored inside the build/test-results/ folder.

This is the configuration that makes GitLab CI/CD store the test results.

  artifacts:
    when: always
    reports:
      junit: build/test-results/test/**/TEST-*.xml

See also the official documentation: Unit test reports

This is the final pipeline that includes unit testing and recording of test results.

stages:
  - info
  - build
  - test

variables:
  GIT_STRATEGY: 'clone'
  COMPILE: 'false'

info:
  stage: info
  script:
    - echo "This is your first stage (ツ)"
    - echo "Username is ${USERNAME} with ${PASSWORD}"

build_application:
  stage: build
  tags:
    - build
    - mobiliar
  image: registry.access.redhat.com/ubi9/openjdk-17:latest
  script:
    - ./gradlew assemble

test_application:
  stage: test
  image: registry.access.redhat.com/ubi9/openjdk-17:latest
  script:
    - ./gradlew check
  artifacts:
    when: always
    reports:
      junit: build/test-results/test/**/TEST-*.xml

Navigate to the newest Pipeline inside GitLab. The Tests tab should now show the executed tests.

Task 6.4: Break a test (Optional)

To show how the pipeline reacts on test failures we change a test to fail.

Open the test class src/test/java/ch/appuio/techlab/controller/PodRestControllerTest.java. Change the assertion from Pod to Hoppla.

        // then
        assertThat(podDescription, startsWith("Hoppla: "));

Commit and push those changes and then check that the pipeline failed and the test results inside the Tests tab have one failure.

java.lang.AssertionError:
Expected: a string starting with "Hoppla: "
     but: was "Pod: 42"

Revert the latest commit to fix the test again.

Last modified September 25, 2023: renabed CI/CD to Build (e01a5c8)