2

我根据指令创建了带有简单 testNG 测试的示例 micronaut 项目。

当我从命令行通过 gradle 运行它时,测试运行良好,但当我尝试从 IDEA 运行它时失败。

为了在日志中成功运行 gradle,我看到了控制器的路由:

    13:40:44.314 [Test worker] DEBUG i.m.web.router.DefaultRouteBuilder - Created Route: GET /hello -> HelloController#String index() (application/json )

IDEA 运行缺少的。

问题:我需要配置什么才能使测试也通过 IDEA?

来源

src/main/kotlin/example/micronaut/Application.kt:

package example.micronaut

import io.micronaut.runtime.Micronaut

object Application {

    @JvmStatic
    fun main(args: Array<String>) {
        Micronaut.build()
                .packages("example.micronaut")
                .mainClass(Application.javaClass)
                .start()
    }
}

src/main/kotlin/example/micronaut/HelloController.kt:

package example.micronaut

import io.micronaut.http.annotation.Controller
import io.micronaut.http.annotation.Get

@Controller("/hello")
class HelloController {

    @Get("/")
    fun index(): String {
        return "Hello World"
    }
}

src/test/kotlin/example/micronaut/HelloTest.kt:

package example.micronaut

import io.micronaut.context.ApplicationContext
import io.micronaut.http.client.HttpClient
import io.micronaut.runtime.server.EmbeddedServer
import org.testng.annotations.AfterMethod
import org.testng.annotations.BeforeMethod
import org.testng.annotations.Test

class HelloTest {

    lateinit var embeddedServer: EmbeddedServer
    lateinit var client: HttpClient

    @BeforeMethod
    fun init() {
        embeddedServer = ApplicationContext.run(EmbeddedServer::class.java)
        client = HttpClient.create(embeddedServer.url)
    }

    @Test
    fun check() {
        val response: String = client.toBlocking().retrieve("/hello")
        assert(response == "Hello World")
    }

    @AfterMethod
    fun cleanup() {
        client.close()
        embeddedServer.close()
    }

}

构建.gradle:

buildscript {
    repositories {
        mavenCentral()
        maven { url "https://plugins.gradle.org/m2/" }
    }
    dependencies {
        classpath "com.github.jengelman.gradle.plugins:shadow:2.0.4"
        classpath "io.spring.gradle:dependency-management-plugin:1.0.5.RELEASE"
        classpath "net.ltgt.gradle:gradle-apt-plugin:0.15"
        classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:1.2.31"
    }
}

version "0.1"
group "example"

apply plugin:"io.spring.dependency-management"
apply plugin:"com.github.johnrengelman.shadow"
apply plugin:"application"
apply plugin:"java"
apply plugin:"net.ltgt.apt-eclipse"
apply plugin:"net.ltgt.apt-idea"
apply plugin:"kotlin"
apply plugin:"kotlin-kapt"


repositories {
    mavenLocal()
    mavenCentral()
    maven { url "https://jcenter.bintray.com" }
}

dependencyManagement {
    imports {
        mavenBom 'io.micronaut:bom:1.0.0.M1'
    }
}

dependencies {
    annotationProcessor "io.micronaut:inject-java"
    compile "io.micronaut:http-client"
    compile "io.micronaut:http-server-netty"
    compile "io.micronaut:inject"
    compile "io.micronaut:runtime"
    compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8:1.2.31"
    compile "org.jetbrains.kotlin:kotlin-reflect:1.2.31"
    compileOnly "io.micronaut:inject-java"
    kapt "io.micronaut:inject-java"
    runtime "ch.qos.logback:logback-classic:1.2.3"

    testCompile "io.micronaut:inject-java"

    testImplementation("org.testng:testng:6.13.1")
}

shadowJar {
    mergeServiceFiles()
}

mainClassName = "example.micronaut.Application"
compileJava.options.compilerArgs += '-parameters'
compileTestJava.options.compilerArgs += '-parameters'

test {
    useTestNG()
}

compileKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

compileTestKotlin {
    kotlinOptions.jvmTarget = "1.8"
}

解决了

如http://mrhaki.blogspot.com/2016/03/gradle-goodness-configure-intellij-idea.html中所述,通过将 gradle 设置为测试运行程序来修复

4

1 回答 1

1

目前需要一个构建工具来构建 Kotlin + Micronaut 应用程序,直到 IntelliJ 为 Kapt 带来原生支持。这在“Kotlin、Kapt 和 IntelliJ”部分中的https://docs.micronaut.io/latest/guide/index.html#kotlin进行了解释

于 2018-07-03T12:13:08.000 回答