---
title: JUnit Integration with Mergify
description: Report your test results from JUnit tests to Mergify
---

This guide shows how to generate JUnit reports from your JUnit tests and upload
them to **Test Engine** using your CI workflow.

## Generate a JUnit report with JUnit

Maven and Gradle both write JUnit XML reports when they run your tests, with no
plugin to add. What changes between them is where the reports land and how
they name your tests (see [Test names](#test-names)).

### Using Maven

Maven's Surefire plugin runs your unit tests and writes one report per test
class in each module's `target/surefire-reports/`:

```bash
mvn -B -fae test
```

`-fae` (fail at end) keeps Maven going after a module's tests fail, so the
other modules run their tests and write their reports too. In a multi-module
build, upload the reports of every module with
`**/target/surefire-reports/TEST-*.xml`.

If you run integration tests with the Failsafe plugin, run `mvn -B -fae verify`
instead, and upload `**/target/*-reports/TEST-*.xml`, one pattern that covers
both plugins' reports. Keep the `TEST-` prefix: Failsafe also writes a
`failsafe-summary.xml` in the same directory, which is not a JUnit report, and
an upload that includes it fails.

### Using Gradle

Gradle's `test` task writes JUnit XML reports without any extra configuration.
If your tests run on JUnit 5, enable the platform in your `build.gradle`:

```groovy
test {
    useJUnitPlatform()
}
```

Run tests with:

```bash
./gradlew test --continue
```

`--continue` keeps Gradle going after a project's tests fail, so the other
projects run their tests and write their reports too. Each project writes one
report per test class in its `build/test-results/test/`. A test task of your
own, such as `integrationTest`, writes to a directory named after it,
`build/test-results/integrationTest/`; upload `**/build/test-results/**/*.xml`
to cover them all.

## Update your CI workflow

The examples below use a Maven build with several modules. For Gradle, run
`./gradlew test --continue` and upload `**/build/test-results/**/*.xml`
instead.

### GitHub Actions

Run the tests in a step with an `id`, and let the upload step decide the
job's result:

```yaml
- name: Run JUnit Tests
  id: tests
  continue-on-error: true
  run: mvn -B -fae test
```

```yaml
- name: Mergify Test Engine Upload
  if: (success() || failure()) && (!github.event.pull_request || github.event.pull_request.head.repo.full_name == github.repository)
  uses: mergifyio/gha-mergify-ci@v35
  with:
    report_path: "**/target/surefire-reports/TEST-*.xml"
    test_step_outcome: ${{ steps.tests.outcome }}
```

Key points:

- `if: (success() || failure()) && (!github.event.pull_request || github.event.pull_request.head.repo.full_name == github.repository)`: runs the upload step even when the tests fail, so **Test Engine** gets the full report, and skips it on pull requests from forks, which get no ID token.
- `report_path: "**/target/surefire-reports/TEST-*.xml"`: where your test run writes the JUnit file. It must match the path your test configuration uses.
- `test_step_outcome: ${{ steps.tests.outcome }}`: passes the test step's outcome, so Mergify detects a runner that crashed while the JUnit report looks clean. Give your test step an `id` (here `tests`) and use it in `steps.<id>.outcome`.

If you use a job matrix in your workflow (e.g., to test across multiple versions), every leg reports the same job name, because GitHub Actions gives them all one `GITHUB_JOB`. Set the `job_name` input so **Test Engine** can tell the reports apart:

```yaml
jobs:
  example_matrix:
    strategy:
      matrix:
        version: [10, 12, 14]
```

Your upload step should look like:

```yaml
- name: Mergify Test Engine Upload
  if: (success() || failure()) && (!github.event.pull_request || github.event.pull_request.head.repo.full_name == github.repository)
  uses: mergifyio/gha-mergify-ci@v35
  with:
    job_name: example_matrix (${{ matrix.version }})
    report_path: "**/target/surefire-reports/TEST-*.xml"
    test_step_outcome: ${{ steps.tests.outcome }}
```

> **Note:** the job name is part of a test's identity, so a scheduled workflow has to report the same name as the job running those tests on your pull requests.

To benefit from Test Engine quarantine, you need to add `continue-on-error: true`
in your GitHub Actions step that executes your tests and generates the JUnit file.
The step running the `gha-mergify-ci` action then decides whether the job passes or fails,
taking quarantined tests into account.

With `continue-on-error: true`, the job's result comes from the upload step,
which pull requests from forks skip: add the step that fails the job when the
tests failed and nothing was uploaded, as shown in
[pull requests from forks](/ci-insights/setup/github-actions#pull-requests-from-forks).

#### Let your CI reach Mergify

Grant `id-token: write` to the job, or to the whole workflow. The job then trades its GitHub-signed ID token for a Mergify OIDC CI token, and no `MERGIFY_TOKEN` is stored:

```yaml
permissions:
  id-token: write # lets the job ask GitHub for an ID token
  contents: read # actions/checkout needs it
```

A `permissions` block sets every permission it does not list to `none`: list the ones your other steps need too.

Mergify refuses the ID token until a trust policy of your organization matches the job. A GitHub organization `Owner` or a **CI Admin** writes one under **Settings → Security & Access → CI Authentication → Trust Policies** on the [dashboard](https://dashboard.mergify.com). [OIDC Authentication](/integrations/gha/oidc) covers trust policies, pull requests from forks and refused exchanges. If your workflows store `MERGIFY_TOKEN` today, [move them off it](/integrations/gha/oidc#move-your-workflows-off-mergify_token).

#### Use an application key instead

Other CIs authenticate with a `ci` [application key](/api/application-keys#ci-reporting), and so do the GitHub Actions jobs OIDC leaves out: runs for pull requests from forks when your organization sends them secrets, and jobs that send scopes from the base branch (`pull_request_target`, `workflow_run`, `issue_comment`). Store the key as the `MERGIFY_TOKEN` repository secret and set it on the job:

```yaml
env:
  MERGIFY_TOKEN: ${{ secrets.MERGIFY_TOKEN }}
```

A `MERGIFY_TOKEN` that is set wins over the ID token. For pull requests from forks, set it on those runs only and drop the fork condition from the upload step, as [Jobs that keep a key](/integrations/gha/oidc#jobs-that-keep-a-key) shows. Store the key in your [Dependabot secrets](/integrations/dependabot#reaching-mergify-from-dependabot-pull-requests) too, since runs for Dependabot pull requests read only those.

### Buildkite

```yaml
steps:
  - label: "Run tests"
    command: <your test command>
    plugins:
      - mergifyio/mergify-ci#v30:
          action: junit-process
          report_path: "**/target/surefire-reports/TEST-*.xml"
```

Key points:

- The plugin runs in the `post-command` hook, so it uploads results **after** your tests finish, even if they fail.
- `report_path: "**/target/surefire-reports/TEST-*.xml"`: where your test run writes the JUnit file. It must match the path your test configuration uses.
- Silent failure detection is automatic: the plugin reads the step's exit code to detect a runner that crashed while the JUnit report looks clean.

If you use a build matrix in your pipeline, each variation reports its own step label as its job name, so the variations are already distinct. Set the `job_name` plugin property when the label is not the name you want **Test Engine** to file the reports under:

```yaml
steps:
  - label: "Flaky detection ({{matrix}})"
    matrix:
      - "3.10"
      - "3.11"
      - "3.12"
    command: <your test command>
    plugins:
      - mergifyio/mergify-ci#v30:
          action: junit-process
          job_name: "Tests ({{matrix}})"
          report_path: "**/target/surefire-reports/TEST-*.xml"
```

> **Note:** the job name is part of a test's identity, so a scheduled pipeline has to report the same name as the step running those tests on your pull requests.

> **Caution: keep the token out of the pipeline you upload.** Set `MERGIFY_TOKEN` in the environment of the agents running your steps: [Buildkite secrets](https://buildkite.com/docs/pipelines/security/secrets/buildkite-secrets), a secrets plugin backed by your own secrets store, or an agent `environment` hook that exports it. Do not put the token in the pipeline itself: `buildkite-agent pipeline upload` interpolates `${MERGIFY_TOKEN}` before the pipeline is stored, so a token written into an `env:` block is uploaded in plain text and stays readable in the Buildkite UI and in build history. The plugin's `token:` property carries it the same way.

When using the Buildkite plugin for Test Engine quarantine, the plugin automatically
detects test failures using the step's exit code. No extra configuration is needed.

The `mergifyio/mergify-ci` plugin runs in the `post-command` hook and reads
`BUILDKITE_COMMAND_EXIT_STATUS` to detect silent failures where the test runner
crashed but the JUnit report appears clean.

### Any CI (Mergify CLI)

Quote the pattern, so that `mergify ci junit-process` expands it rather than
the shell: without `globstar`, bash reads `**` as `*` and misses the reports
at other depths.

Install the [Mergify CLI](/cli/usage) in your pipeline and export `MERGIFY_TOKEN`. Run `mergify ci junit-process` after your tests:

```bash
set -o pipefail
exit_code=0
mvn -B -fae test || exit_code=$?
mergify ci junit-process \
  --test-exit-code "$exit_code" \
  "**/target/surefire-reports/TEST-*.xml"
```

Key points:

- `MERGIFY_TOKEN`: export this environment variable with your Mergify application key so `junit-process` can authenticate to the API. On GitHub Actions, a job granted `id-token: write` needs none: it trades its ID token for a Mergify OIDC CI token, as [OIDC Authentication](/integrations/gha/oidc) explains.
- `|| exit_code=$?`: CI shells run with `-e`, so a failing test command on its own line would end the script before the upload. This form keeps going and records the code.
- The script needs bash for `set -o pipefail`: on Jenkins, whose `sh` steps often run `dash`, start it with `#!/bin/bash`.
- `--test-exit-code "$exit_code"`: passes the test runner's exit code, so Mergify detects a runner that crashed while the JUnit report looks clean.
- `junit-process` runs last, so its status becomes the step's: `0` when every failure it found is quarantined, `1` when one is not.
- The branch quarantine is evaluated against is detected from your CI provider, falling back to the checked-out branch. Pass `--tests-target-branch <branch>` only to override it.

## Test names

Test Engine names each test by its class's full name, then a dot and the name
the build tool reports for it. Maven and Gradle report different names for the
same test, so moving a project from one to the other starts new histories in
Test Engine.

- `@Test void addsAnItem()` in `com.example.CartTest`: Maven reports
  `com.example.CartTest.addsAnItem`, Gradle `com.example.CartTest.addsAnItem()`.

- The same test with `@DisplayName("adds an item")`: Maven reports
  `com.example.CartTest.addsAnItem`, Gradle `com.example.CartTest.adds an item`.

- The first case of `@ParameterizedTest void positive(int n)`: Maven reports
  `com.example.CartTest.positive(int)[1]`, Gradle `com.example.CartTest.[1] 1`.

- `@Test void pays()` in the `@Nested` class `Checkout`: Maven reports
  `com.example.CartTest$Checkout.pays`, Gradle
  `com.example.CartTest$Checkout.pays()`.

With Gradle, a parameterized test's cases are named by the test's display
name pattern, which leaves out the method's name by default. Give the pattern
the method's name, so the cases of two parameterized tests in one class keep
separate names:

```java
@ParameterizedTest(name = "positive({0})")
@ValueSource(ints = {1, 2})
void positive(int n) { /* ... */ }
```

The first case is then reported as `com.example.CartTest.positive(1)`.

Renaming a class or a method, or with Gradle a display name, starts a new
history for the tests it holds. Tests marked with `@Disabled` are reported as
skipped.

## Verify and review in Test Engine

After pushing these changes:

1. Your CI workflow runs your JUnit tests.
2. Maven or Gradle writes a report per test class.
3. Mergify uploads the reports to Test Engine.

You can then review your test results, including any failures or flaky tests,
directly in the [Test Engine
dashboard](https://dashboard.mergify.com/test-engine/detection).

## Troubleshooting tips

<ul>
  <li>
    Upload fails with Failsafe: an upload pattern such as `target/failsafe-reports/*.xml` picks
    up `failsafe-summary.xml`. Match `TEST-*.xml` instead.
  </li>

  <li>
    Reports of one module only: without `-fae`, Maven stops at the first module whose tests
    fail, and the modules after it write no report. Gradle does the same without `--continue`.
  </li>

  <li>The CLI provides information about the upload. Check the logs in GitHub Actions.</li>
<li>File paths: Double-check that the output file matches the path used in `report_path`.</li>
<li>
  Authentication: The upload step's log says why Mergify refused the job's ID token, as [OIDC
  troubleshooting](/integrations/gha/oidc#troubleshooting) details. A job that sets
  `MERGIFY_TOKEN` uses that key instead: check that it is valid and granted [CI
  Reporting](/api/application-keys#ci-reporting), or [move the job off
  it](/integrations/gha/oidc#move-your-workflows-off-mergify_token).
</li>
<li>Workflow conditions: If your step is not running, confirm the if condition is actually triggered in your job.</li>
</ul>
