0

在我的应用程序中,我想测试我放入 buildSrc 代码中的业务逻辑。

我的应用程序的构建是这样的:

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

我在根级别有 build.gradle.kts

// 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)
}

应用级别的 build.gradle.kts

和 buildSrc 级别的 build.gradle.kts

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"
}

问题是我无法在我的测试类中导入 jUnit5 类:

package mypackage

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

我无法导入我的 jUnit 5 测试类。

在此处输入图像描述

请帮忙 :D

4

0 回答 0