Hervé Darritchon
Hervé Darritchon

Reputation: 461

Android Studio - Test Business Logic in buildSrc (Gradle Kotlin DSL)

In my app, I 'd like to test the business logic I put in the buildSrc Code.

The construction of my app is like that :

+ root
  +app
    + src
      + main
        + kotlin
        + res
      + test
        + kotlin
  + buildSrc
    + src
      + main
        + kotlin
      + test
        + kotlin

I have build.gradle.kts at root level

// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
    val kotlin_version = "1.4.10"
    repositories {
        google()
        jcenter()
    }
    dependencies {
        classpath ("com.android.tools.build:gradle:4.1.0")
        classpath ("org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version")

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
    }
}

tasks.register ("clean", Delete::class) {
    delete (rootProject.buildDir)
}

a build.gradle.kts at app level

and a build.gradle.kts at buildSrc level

plugins {
    kotlin("jvm") version "1.3.31"
}

repositories {
    jcenter()
}

dependencies {
    implementation(kotlin("stdlib-jdk8"))
    testImplementation(platform("org.junit:junit-bom:5.7.0"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

tasks.getByName<Test>("test"){
    useJUnitPlatform()
    testLogging {
        events("passed", "skipped", "failed")
    }
}

// config JVM target to 1.8 for kotlin compilation tasks
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile>().configureEach {
    kotlinOptions.jvmTarget = "1.8"
}

The problem is that I can't import jUnit5 class in my test Class :

package mypackage

class MyServiceTest {
    @Test
    fun `1 + 1 = 2`() {
        assertEquals(2, 1 + 1, "1 + 1 should equal 2")
    }
}

I can't import my jUnit 5 test classes.

enter image description here

Please help :D

Upvotes: 3

Views: 793

Answers (0)

Related Questions