skip to content

What error cases and edge conditions should an IDE integration handle when using TestLauncher (no matching tests, version compatibility, failure reporting)?

level: seniorimportance: nice to knowfreq 18%

answer

  1. no-match selection => run fails
  2. TestExecutionException / UnsupportedVersionException
  3. use TEST events for per-test results
  4. BuildEnvironment for version gating
  5. CancellationToken + async ResultHandler

basics

~20 s

Handle 'no matching tests' (selection matched nothing fails the run), test failures surfaced as a build failure with details from TEST events, and Tooling-API-vs-Gradle version compatibility. Always capture events for accurate reporting and propagate exceptions to the UI.

solid answer

~40 s

Robust TestLauncher integrations defend against several conditions. **No matching tests:** if your selection matches no test in any Test task, the run **fails** (intentionally, to catch typos) — surface a clear message instead of a generic build error. **Test failures:** `run()` throws a `BuildException`/`TestExecutionException`; rely on captured TEST `ProgressListener` events (TestFinishEvent results) for per-test detail rather than parsing the exception. **Version compatibility:** the Tooling API can drive a range of Gradle versions, but some APIs (e.g. parts of TestLauncher) require a minimum Gradle version; guard with the build environment model and handle `UnsupportedVersionException`/`UnsupportedBuildArgumentException`. **Cancellation:** wire a `CancellationToken` so the IDE's stop button aborts the run. **Output and async:** redirect standard output/error and consider `run(ResultHandler)` to keep the UI responsive. Treating these as first-class makes the integration feel native.

code

kotlin · 11 lines
kotlin
try {
    connection.newTestLauncher()
        .withJvmTestClasses("com.acme.FooTest")
        .setStandardOutput(System.out)
        .run()
} catch (e: TestExecutionException) {
    // includes the 'no matching tests' case and test failures
    showInIde("Test run failed: " + e.message)
} catch (e: UnsupportedVersionException) {
    showInIde("This Gradle version doesn't support the requested feature")
}

go deeper

for a junior

Know that a bad selection fails and that failures surface as exceptions.

for a middle

Catch the relevant exceptions and use TEST events for accurate reporting.

for a senior

Handle version gating, cancellation, async runs, and the no-match case explicitly.

for a principal

Define the integration's error-handling and compatibility contract across supported Gradle versions and IDE UX states.

## The exceptions you'll actually see - **`TestExecutionException`** — thrown when test execution itself fails, including the 'no matching tests for selection' case. Catch it and translate to a precise UI message. - **`BuildException`** — a general build failure (e.g. compilation error before tests run). - **`UnsupportedVersionException`** — the connected Gradle version is too old for an API you used. - **`UnsupportedBuildArgumentException`** — bad arguments passed to the launcher. - **`BuildCancelledException`** — surfaced when a cancellation token is triggered. ## No matching tests Selection that matches nothing **fails on purpose**, so a mistyped class name doesn't masquerade as a green run. Your integration should detect this specific failure and tell the user 'no tests matched X' rather than a generic red bar. Combine with captured events to confirm zero tests started. ## Don't parse exceptions for results — use events For accurate per-test pass/fail reporting, register a TEST `ProgressListener` and build the result tree from `TestStartEvent`/`TestFinishEvent`. The thrown exception tells you the overall run failed; the events tell you *which* tests and *why*. This is more reliable and localizes failures in the UI. ## Version and environment guarding Use the `BuildEnvironment` model (`connection.model(BuildEnvironment::class.java)`) to read the Gradle version and decide whether a feature is available. Some TestLauncher capabilities (and `withTestsFor`/structured specs) require newer Gradle; gate them and fall back gracefully, catching `UnsupportedVersionException`. ## Cancellation and responsiveness ```kotlin val cancellation = GradleConnector.newCancellationTokenSource() connection.newTestLauncher() .withJvmTestClasses("com.acme.FooTest") .withCancellationToken(cancellation.token()) .run(object : ResultHandler<Void> { override fun onComplete(result: Void?) { /* update UI */ } override fun onFailure(failure: GradleConnectionException) { /* show error */ } }) // later, on Stop button: cancellation.cancel() ``` ## Output streams Always set `setStandardOutput`/`setStandardError` (and optionally `setColorOutput(false)` for clean capture) so test logs reach the IDE console. ## Summary checklist - Detect and message 'no matching tests'. - Build results from TEST events, not exception text. - Guard features by Gradle version; catch UnsupportedVersionException. - Wire cancellation; prefer async run(ResultHandler). - Redirect stdout/stderr.

  • Why does Gradle fail the run when a selection matches no tests?
    To prevent typos from silently 'passing' — a non-matching class name producing zero tests would otherwise look like success.
  • Should you parse the thrown exception to report which tests failed?
    No — build the per-test result tree from TEST ProgressListener events; the exception only signals overall failure.
  • How do you keep the integration from breaking on older Gradle versions?
    Read the Gradle version via the BuildEnvironment model, gate newer TestLauncher features, and catch UnsupportedVersionException for graceful fallback.

  • Assuming an empty selection is a no-op success — it fails the run.
  • Scraping exception messages for per-test results instead of using events.
  • Ignoring Tooling-API-vs-Gradle version compatibility.

context