skip to content

How does `--test-framework` work in `gradle init`, and which frameworks are available?

level: middleimportance: should knowfreq 35%

answer

  1. junit (4) / junit-jupiter (5) / testng / spock
  2. adds testImplementation deps
  3. configures test runner: useJUnitPlatform / useJUnit / useTestNG
  4. emits a sample test
  5. framework depends on type/language

basics

~10 s

--test-framework picks the testing library wired into the generated build and sample test—e.g. junit (JUnit 4), junit-jupiter (JUnit 5), testng, spock (Groovy), or kotlintest. It adds the right dependencies and configures tasks.test (e.g. useJUnitPlatform()).

solid answer

~40 s

`--test-framework` selects the test framework for the scaffolded project. The valid values depend on the `--type`/language: for JVM projects you typically get `junit` (JUnit 4), `junit-jupiter` (JUnit 5 / JUnit Platform), `testng`, and—for Groovy types—`spock`; Kotlin types also offer `kotlin.test`. The flag does three things in the generated `build.gradle(.kts)`: adds the framework's `testImplementation`/`testRuntimeOnly` dependencies, configures the `test` task to use the right runner (`useJUnitPlatform()` for Jupiter/TestNG-on-platform, `useJUnit()` for JUnit 4, `useTestNG()` for TestNG), and emits a sample test class using that framework's API. If you omit the flag, `init` prompts (defaulting per type—modern JVM scaffolds default to `junit-jupiter`). For `--type basic` the flag is irrelevant because no test source is generated.

code

bash · 8 lines
bash
# JUnit 5
gradle init --type java-library --dsl kotlin --test-framework junit-jupiter

# JUnit 4 (legacy)
gradle init --type java-library --dsl kotlin --test-framework junit

# Spock (Groovy)
gradle init --type groovy-library --dsl groovy --test-framework spock

go deeper

for a junior

Know the flag picks a test library and names a couple (JUnit, JUnit 5).

for a middle

List the values, map them to runner config (useJUnitPlatform/useJUnit/useTestNG), and note language constraints.

for a senior

Explain the three coordinated changes (deps, runner, sample) and the move to JvmTestSuite for declarative config.

for a principal

Standardize a test framework across new repos and account for migration when legacy JUnit 4 scaffolds linger.

## Purpose `--test-framework` chooses which testing library the generated project is wired up with. It's only meaningful for types that produce test source (so not `basic`). ## Available values (JVM) - `junit` — **JUnit 4**. Generates a `@Test`-annotated class and configures `test { useJUnit() }`. - `junit-jupiter` — **JUnit 5** on the JUnit Platform. Adds `org.junit.jupiter:junit-jupiter` and configures `test { useJUnitPlatform() }`. The modern default for new JVM builds. - `testng` — **TestNG**, configured via `useTestNG()`. - `spock` — **Spock** (Groovy-based BDD), available for Groovy/JVM types; runs on the JUnit Platform. - `kotlin.test` — for Kotlin types, wires the `kotlin-test` library. ## What it changes in the build For each framework, `init` does three coordinated things: 1. **Dependencies** — e.g. `testImplementation("org.junit.jupiter:junit-jupiter")` and a `testRuntimeOnly("org.junit.platform:junit-platform-launcher")` for Jupiter. 2. **Test task runner** — sets `tasks.named<Test>("test") { useJUnitPlatform() }` (or `useJUnit()` / `useTestNG()`). 3. **Sample test** — emits a test class written against that framework's annotations/assertions. ```kotlin // Generated for --test-framework junit-jupiter (Kotlin DSL) dependencies { testImplementation("org.junit.jupiter:junit-jupiter:5.10.0") testRuntimeOnly("org.junit.platform:junit-platform-launcher") } tasks.named<Test>("test") { useJUnitPlatform() } ``` ## Defaults & omission Without the flag, `init` prompts and applies a sensible default per type—modern JVM templates default to `junit-jupiter`. Newer Gradle versions also use the `JvmTestSuite` (`testing { suites { ... } }`) block to express the framework declaratively. ## Gotchas - Spock is only valid with Groovy-capable types. - Picking JUnit 4 (`junit`) gives you the legacy `useJUnit()` runner, not the platform—relevant if you later add Jupiter tests.

  • What test-task configuration does `junit-jupiter` produce versus `junit`?
    `junit-jupiter` configures `test { useJUnitPlatform() }`; `junit` (JUnit 4) configures `test { useJUnit() }`, the legacy runner.
  • Why doesn't `--test-framework` matter for `--type basic`?
    `basic` generates no source or test directories, so there's nothing to wire a test framework into.

saying these in an interview costs you the question

  • Confusing `junit` (JUnit 4) with `junit-jupiter` (JUnit 5)—they configure different runners.
  • Saying Spock works with any type—it requires a Groovy-capable type.

context