科特林。 Micronaut。在IDEA中,TestNG测试失败,但是通过gradle传递

时间:2018-06-01 11:30:29

标签: intellij-idea kotlin testng micronaut

我已经使用基于this指令的简单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 /主/科特林/示例/ 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 /主/科特林/示例/ 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 /测试/科特林/示例/ 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()
    }

}

的build.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"
}

解决

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

中所述

1 个答案:

答案 0 :(得分:1)

当前需要使用构建工具来构建Kotlin + Micronaut应用程序,直到IntelliJ为Kapt带来本机支持。 https://docs.micronaut.io/latest/guide/index.html#kotlin在“ Kotlin,Kapt和IntelliJ”部分对此进行了解释