Search code examples
kotlinandroid-gradle-pluginbuild.gradlegradle-kotlin-dsl

Unit tests failed after converting gradle to kotlin dsl


Converted my gradle file to a kts to use kotlin dsl. Im not sure how to convert these two sections:

sourceSets {
    test.resources.srcDirs += 'src/test/res'
}

testOptions {
    unitTests.all {
        useJUnitPlatform()
    }
}

All my unit testand instrumentation tests ar enow failing because it cant locate the test data anymore. Could someone help me convert the above two snippets into dsl for the kts gradle file?

I've tried:

java.sourceSets.create("src/test/res")

sourceSets {
    named("test") {
        java.srcDir("src/test/res")
    }
}

However both did not work.


Solution

  • After more research and trial and error I found this to work:

    sourceSets {
       getByName("test").resources.srcDir("src/test/res")
    }
    
     testOptions {
        unitTests.all {
            it.useJUnitPlatform()
        }
    }