ludenus
ludenus

Reputation: 1341

Kotlin. Micronaut. TestNG test fails in IDEA but pass via gradle

I have created sample micronaut project with simple testNG test based on this instruction.

The test works perfectly when I run it via gradle from command line, but fails when I try to run it from IDEA.

For successful gradle run in logs I see routing for my controller:

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

which is missing for IDEA run.

QUESTION: What do I need to configure to make the test also pass in IDEA?

sources

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

}

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

Solved

fixed by setting gradle as test runner as described in http://mrhaki.blogspot.com/2016/03/gradle-goodness-configure-intellij-idea.html

Upvotes: 3

Views: 998

Answers (1)

Graeme Rocher
Graeme Rocher

Reputation: 7985

Currently a build tool is required to build Kotlin + Micronaut applications until IntelliJ brings native support for Kapt. This is explained here https://docs.micronaut.io/latest/guide/index.html#kotlin in the section "Kotlin, Kapt and IntelliJ"

Upvotes: 2

Related Questions