繁体   English   中英

Kotlin测试无法从命令行通过ClassNotFoundException进行失败,但可以从IntelliJ进行

[英]Kotlin tests fail from command line with ClassNotFoundException but work from IntelliJ

我在这里有一个Kotlin Spring Boot项目。 它具有一些测试,这些测试可以从IntelliJ正常运行,但是当我从命令行运行时,失败并出现以下错误。

BUILD FAILED in 1m 12s
7 actionable tasks: 7 executed
asarkar:license-report-kotlin$ ./gradlew clean test

> Task :compileKotlin
Using kotlin incremental compilation

> Task :compileTestKotlin
Using kotlin incremental compilation

> Task :test

2017-07-16 21:43:06.345  INFO 2956 --- [      Thread-13] s.c.a.AnnotationConfigApplicationContext : Closing org.springframework.context.annotation.AnnotationConfigApplicationContext@54fa5525: startup date [Sun Jul 16 21:42:04 PDT 2017]; root of context hierarchy
org.abhijitsarkar.ApplicationTest > testEndToEnd FAILED
    java.lang.AssertionError at ApplicationTest.kt:83

org.abhijitsarkar.service.GradleAgentTest > initializationError FAILED
    java.lang.ClassNotFoundException

org.abhijitsarkar.service.JGitAgentTest > initializationError FAILED
    java.lang.ClassNotFoundException

org.abhijitsarkar.service.LinkVerifierTest > initializationError FAILED
    java.lang.ClassNotFoundException

4 tests completed, 4 failed


FAILURE: Build failed with an exception.

到目前为止,我已经尝试过:

  1. build.gradlebuild.gradle.kts之间来回build.gradle.kts
  2. 将Kotlin运行时添加到jar kotlinOptions.includeRuntime = true
  3. 将包级别的函数更改为对象中的函数。

我发现很奇怪:

  1. 除了通常的Gradle build目录外,还将创建一个out目录。
  2. 一些类/对象被编译为名称以Kt结尾的类文件。 我还没有找到任何押韵或理由,但是我对Kotlin还是陌生的,所以我可能会缺少一些东西。

您的测试(或实际测试的类)做了一些奇怪的事情:它们以某种方式删除了包含测试类的build文件夹的内容。

要查看此内容,请尝试以下操作:

./gradlew clean testClasses
find build    # You get a full list of all main and test classes
./gradlew clean testClasses test
find build    # You get a very limited list of resources in the build-folder (must be somehow modified by your code)

如果您停用所有测试,然后在其中添加以下内容(仅出于测试目的):

import io.kotlintest.matchers.*
import io.kotlintest.specs.*

class MyTests : StringSpec() {
  init {

    "length should return size of string" {
      "hello".length shouldBe 5
    }

  }
}

如果您再次进行gradle-test,

./gradlew clean testClasses test
find build    # all resources again available

检查您的测试是否有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM