https://kotlinlang.org logo
#gradle
Title
# gradle
k

Ky

07/15/2022, 4:53 PM
Hey Guys, I'm having trouble running Junit tests in the buildSrc test directory. The tests are not even being recognized, there is no run button next to method annotated with @Test. I've been searching for samples that display setting up simple Unit tests in the buildSrc directory. I've been following gradles documentation on this but I'm at a point with no information output that gives me a idea of what i'm doing wrong Any suggestions?
c

Chris Lee

07/15/2022, 4:54 PM
is junit included in the buildSrc/build.gradle? Is junit enabled for testing in build.gradle?
Generally something like this.
k

Ky

07/15/2022, 4:59 PM
@Chris Lee Yes my buildSrc/build.gradle.kts is similar
Copy code
plugins {
    `java-gradle-plugin`
    java
    `kotlin-dsl`
}



sourceSets {
    main { java { srcDirs("src") } }
    test { java { srcDirs("src") } }
}

repositories {
    mavenCentral()

}

dependencies {
    implementation(localGroovy())
    implementation(gradleApi())

    implementation("com.squareup.moshi:moshi:1.8.0")
    implementation("com.squareup.moshi:moshi-kotlin:1.8.0")
    // CSV helper library for CombineDataSafetyCSV.kt
    implementation("org.apache.commons:commons-csv:1.5")
    testImplementation("junit:junit:4.13.2")
}



    resolutionStrategy.cacheChangingModulesFor(0, TimeUnit.SECONDS)
    resolutionStrategy.eachDependency {
        if (requested.group == "org.jetbrains.kotlin"
        ) {
            // can't resolve AppLibraries.KOTLIN_VERSION
            useVersion("1.6.10")
        }
    }
}

tasks.test {
    useJUnit()
    println("LOOK HERE: " + testClassesDirs.files)
    println("WORKING DIR: " + workingDir)
    // Propagate all system properties in order to access them in tests.
    systemProperties(System.getProperties() as Map<String, Any>)
}
c

Chris Lee

07/15/2022, 5:01 PM
Any reason for using the legacy Junit 4 instead of Junit 5?
k

Ky

07/15/2022, 5:02 PM
No, just trying different things as I also attempted with JUnit5. One thing I'm noticing is that the build output has same classes in main and test
c

Chris Lee

07/15/2022, 5:03 PM
suggest removing the custom source sets, they aren’t required - place source in the conventional locations,
src/main/java
,
src/test/java
☝️ 1
k

Ky

07/15/2022, 5:52 PM
Tests are being recognized w/ JUnit5.
👍 1
g

gildor

07/16/2022, 2:38 PM
buildSrc tests are in general works and configured (out of the box you even don't need any additional setup except junit) exactly the same as any other module
About your second question, If it is just a custom plugin with a task, you should use Gradle TestKit to run tests against it, so Gradle run from tests and assert results