Skip to content

Fix tests in Gradle 9 #68

@trask

Description

@trask

Convert Custom Test Tasks to Modern Test Suites for Gradle 9 Compatibility

Background & Problem Context

The overnight metadata job uncovered that some of our test suites might not be running.

After upgrading to Gradle 9, custom Test tasks registered with by registering(Test::class) are being marked as "NO-SOURCE" and failing to execute tests. This is due to Gradle 9's new requirement that Test tasks must have explicit test source configuration.

Current Issue

  • Custom test tasks like testStableSemconv show "NO-SOURCE" and are skipped
  • Tests that previously ran successfully now don't execute
  • Affects ~102 build files across the repository

Solution: Migrate to Modern Test Suites

Instead of applying workarounds to custom Test tasks, we should migrate to Gradle's modern Test Suites approach.

How to Identify Files That Need Updates

Search for files containing custom Test task registrations:

grep -r "by registering(Test::" --include="*.kts" .

This will return ~102 files like:

instrumentation/alibaba-druid-1.0/javaagent/build.gradle.kts:25:  val testStableSemconv by registering(Test::class) {

Example Migration

Before (Current - Broken in Gradle 9):

val collectMetadata = findProperty("collectMetadata")?.toString() ?: "false"

tasks {
  val testStableSemconv by registering(Test::class) {
    jvmArgs("-Dotel.semconv-stability.opt-in=database")
    systemProperty("collectMetadata", collectMetadata)
    systemProperty("metadataConfig", "otel.semconv-stability.opt-in=database")
  }

  test {
    systemProperty("collectMetadata", collectMetadata)
  }

  check {
    dependsOn(testStableSemconv)
  }
}

After (Modern Test Suites - Works with Gradle 9+):

val collectMetadata = findProperty("collectMetadata")?.toString() ?: "false"

testing {
  suites {
    // Configure the default test suite
    named<JvmTestSuite>("test") {
      targets {
        all {
          testTask.configure {
            systemProperty("collectMetadata", collectMetadata)
          }
        }
      }
    }
    
    // Create custom test suite for stable semconv
    val testStableSemconv by registering(JvmTestSuite::class) {
      targets {
        all {
          testTask.configure {
            jvmArgs("-Dotel.semconv-stability.opt-in=database")
            systemProperty("collectMetadata", collectMetadata)
            systemProperty("metadataConfig", "otel.semconv-stability.opt-in=database")
          }
        }
      }
    }
  }
}

tasks {
  check {
    dependsOn(testing.suites.named("testStableSemconv"))
  }
}

Migration Patterns

Pattern 1: Simple Test Task

// OLD
val testStableSemconv by registering(Test::class) {
  jvmArgs("-Dotel.semconv-stability.opt-in=database")
}

// NEW  
val testStableSemconv by registering(JvmTestSuite::class) {
  targets {
    all {
      testTask.configure {
        jvmArgs("-Dotel.semconv-stability.opt-in=database")
      }
    }
  }
}

Pattern 2: Test Task with Filters

// OLD
val testReceiveSpansDisabled by registering(Test::class) {
  filter {
    includeTestsMatching("SuppressReceiveSpansTest")
  }
  include("**/SuppressReceiveSpansTest.*")
}

// NEW
val testReceiveSpansDisabled by registering(JvmTestSuite::class) {
  targets {
    all {
      testTask.configure {
        filter {
          includeTestsMatching("SuppressReceiveSpansTest")
        }
        include("**/SuppressReceiveSpansTest.*")
      }
    }
  }
}

Pattern 3: Multiple Test Tasks in Same File

Convert each by registering(Test::class) to by registering(JvmTestSuite::class) and wrap configurations in targets.all.testTask.configure { }.

Key Migration Rules

  1. Replace: by registering(Test::class)by registering(JvmTestSuite::class)
  2. Wrap configurations: All test task configurations go inside targets.all.testTask.configure { }
  3. Update dependencies: Change dependsOn(taskName)dependsOn(testing.suites.named("taskName"))
  4. Preserve all existing configurations: JVM args, system properties, filters, etc.

Validation Steps

For each converted file, validate the fix works:

1. Syntax Check

./gradlew :module:help --dry-run

Should complete without errors.

2. Task Execution Test

./gradlew :module:testStableSemconv --rerun-tasks

Should execute tests successfully (not show "NO-SOURCE").

Files to Convert

Run this command to get the complete list:

grep -l "by registering(Test::" --include="*.kts" -r . | wc -l

Example files that need conversion:

  • instrumentation/alibaba-druid-1.0/javaagent/build.gradle.kts
  • instrumentation/aws-sdk/aws-sdk-2.2/javaagent/build.gradle.kts
  • instrumentation/spring/spring-boot-autoconfigure/build.gradle.kts
  • instrumentation/hibernate/hibernate-6.0/javaagent/build.gradle.kts
  • And many others...

Success Criteria

  • All custom Test tasks converted to JvmTestSuite
  • All test suites execute successfully (no "NO-SOURCE" errors)
  • No regression in test functionality

References

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions