Commit 1b9601ab authored by Alexandre Levy's avatar Alexandre Levy Committed by Thomas Boni
Browse files

Java JUnit test job

parent 018cd29f
Loading
Loading
Loading
Loading
+122 −0
Original line number Diff line number Diff line
## Objective

Test your Java project using [Apache Maven](http://maven.apache.org/) JDK 11, JaCoCo and Surefire Maven plugins for code coverage and tests report directly in your merge requests.

## How to use it

1. Make sure your Java project is set up to use Maven. You can refer to the [Maven quick start guide](http://maven.apache.org/guides/getting-started/index.html)
2. Use the provided `pom.xml` in the example below to get you started. You are then free to customize your `pom.xml`
depending on your needs. You can also use this job along with the `maven_build` job on the R2DevOps hub for a completely
automated Java pipeline. 
3. Add the job URL inside the `include` list of your `.gitlab-ci.yml` file  (see the [quick setup](/use-the-hub/#quick-setup)). You can specify [a fixed version](#changelog) instead of `latest`.
    ```yaml
      - remote: 'https://jobs.r2devops.io/latest/junit_test.yml'
    ```
4. If you need to customize the job (stage, variables, ...) check the [jobs-customization](/use-the-hub/#jobs-customization)
5. Grab a ☕ while the job is running !

## Example pom.xml
```xml
<project xmlns = "http://maven.apache.org/POM/4.0.0" 
  xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0 
  http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.myorg.myproject</groupId>
<artifactId>myartifact</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>

<properties>
  <junit.version>4.12</junit.version>
</properties>

<dependencyManagement>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</dependencyManagement>

<dependencies>
  <dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <scope>test</scope>
  </dependency>
</dependencies>

<build>
  <directory>${artifactsDirectory}</directory>
  <pluginManagement>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
      </plugin>
      <plugin>
        <groupId>org.jacoco</groupId>
        <artifactId>jacoco-maven-plugin</artifactId>
        <version>0.8.6</version>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>3.0.0-M5</version>
      </plugin>
    </plugins>
  </pluginManagement>
  <plugins>
    <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-compiler-plugin</artifactId>
      <configuration>
        <source>11</source>
        <target>11</target>
      </configuration>
    </plugin>
    <plugin>
      <groupId>org.jacoco</groupId>
      <artifactId>jacoco-maven-plugin</artifactId>
      <executions>
        <execution>
          <id>prepare-agent</id>
          <goals>
            <goal>prepare-agent</goal>
          </goals>
        </execution>
        <execution>
          <id>generate-code-coverage-report</id>
          <phase>test</phase>
          <goals>
            <goal>report</goal>
          </goals>
        </execution>
      </executions>
    </plugin>
  </plugins>
</build>
</project>
```

## Job details

* Job name: `junit_test`
* Docker image: [maven:3.6.3-jdk-11](https://hub.docker.com/_/maven)
* Default stage: `static_tests`
* When: `always`

### Variables
| Name | Description | Default |
| ---- | ------------| ------- |
| `ARTIFACTS_DIR` | Customize the path where the artifacts will be created | `${CI_PROJECT_DIR}/artifacts` |

### Artifacts

We use [Junit](https://junit.org/junit5/){:target="_blank"}'s XML report to display error report
directly in pipeline `Test` tab and in merge request widget. It's also available directly in the artifacts.
+13 −0
Original line number Diff line number Diff line
name: junit_test
description: Run unit tests of a Java project using Maven and JUnit
default_stage: static_tests
icon: 🧪
maintainer: alexlevy
license: MIT
labels:
  - Java
  - Test
images:
  'maven': '3.6.3-jdk-11'
tools:
  maven: '3.6.3'
+22 −0
Original line number Diff line number Diff line
stages:
  - static_tests

junit_test:
  image:
    name: maven:3.6.3-jdk-11
    entrypoint: [""]
  stage: static_tests
  variables:
    ARTIFACTS_DIR: ${CI_PROJECT_DIR}/artifacts
  script:
      - mkdir -p ${ARTIFACTS_DIR}
      - mvn -B clean test -DartifactsDirectory=${ARTIFACTS_DIR}
      - cat ${ARTIFACTS_DIR}/site/jacoco/jacoco.csv | awk -F"," '{ instructions += $4 + $5; covered += $5 } END { print covered, "/", instructions, "instructions covered"; print 100*covered/instructions, "% covered" }'
  artifacts: 
    when: always
    paths:
      - "${ARTIFACTS_DIR}/surefire-reports/TEST-*.xml"
    reports:
      junit:
        - "${ARTIFACTS_DIR}/surefire-reports/TEST-*.xml"
  coverage: '/^(\d+.\d+\ %) covered$/'
+0 −0

Empty file added.

+1 −0
Original line number Diff line number Diff line
* Initial version