gradle/gradle · error · GradleException

There was a problem while executing the tests.

Error message

There was a problem while executing the tests.

What it means

JUnit can report failures against Description.TEST_MECHANISM - failures in the test machinery itself that are not attributable to any single test (exceptions from RunListeners, filter crashes, framework-internal errors). The executor's listener collects these, and rethrowErrors() throws GradleException('There was a problem while executing the tests.') with the first such failure as cause once the run finishes.

Source

Thrown at platforms/jvm/testing-jvm-infrastructure/src/main/java/org/gradle/api/internal/tasks/testing/junit/JUnitTestExecutor.java:192

        junit.run(request);
        errorCollectingListener.rethrowErrors();
    }

    private static class ErrorCollectingListener extends RunListener {
        private final List<Throwable> errors = new ArrayList<>();

        @Override
        public void testFailure(Failure failure) {
            if (failure.getDescription().equals(Description.TEST_MECHANISM)) {
                errors.add(failure.getException());
            }
        }

        void rethrowErrors() {
            if (!errors.isEmpty()) {
                Throwable first = errors.get(0);
                if (errors.size() == 1) {
                    throw new GradleException("There was a problem while executing the tests.", first);
                } else {
                    throw new DefaultMultiCauseException("There were multiple problems while executing the tests.", errors);
                }
            }
        }
    }

    // https://github.com/gradle/gradle/issues/2319
    public static boolean isNestedClassInsideEnclosedRunner(Class<?> testClass) {
        if (testClass.getEnclosingClass() == null) {
            return false;
        }

        Class<?> outermostClass = testClass;
        while (outermostClass.getEnclosingClass() != null) {
            outermostClass = outermostClass.getEnclosingClass();
        }

View on GitHub (pinned to 534f27719b)

Solutions

  1. Inspect the cause throwable - it is the exception your listener/machinery threw, not a test failure
  2. Harden or remove custom testListeners / beforeTest/afterTest hooks so they never throw
  3. Align JUnit versions (single junit:junit 4.13.x on the classpath, evict old transitive copies)
  4. If the cause is environmental (unreachable reporting server), fix that dependency before rerunning tests

Example fix

// before: a throwing listener becomes a whole-run failure
public class MetricsListener extends RunListener {
    @Override public void testStarted(Description d) {
        metrics.push(d.getDisplayName()); // throws when server is down
    }
}
// after: never throw from listener callbacks
public class MetricsListener extends RunListener {
    @Override public void testStarted(Description d) {
        try { metrics.push(d.getDisplayName()); }
        catch (Exception e) { System.err.println("metrics dropped: " + e); }
    }
}
Defensive patterns

Strategy: try-catch

Try / catch

catch (GradleException e) {
    if (e.message == 'There was a problem while executing the tests.') {
        logger.error "test machinery failure, cause: ${e.cause}" // listener/framework issue, not a test assertion
    } else throw e
}

Prevention

When it happens

Trigger: A custom RunListener (or TestWatcher) registered on the Test task throws from a callback; a Filter throws during shouldRun; JUnit internal machinery fails - and exactly one such TEST_MECHANISM failure is recorded during the run.

Common situations: Listeners doing network/file I/O that fails (reporting servers, logging hooks); listeners incompatible with the JUnit version on the classpath; mixed JUnit versions where the runner machinery calls a missing method.

Related errors


AI-assisted analysis of gradle/gradle@534f27719b (2026-08-22). Data as JSON: /api/errors/4639c4edd4dc4f69. Report an issue: GitHub.