【问题标题】:Kotlin tests fail from command line with ClassNotFoundException but work from IntelliJKotlin 测试从命令行失败并出现 ClassNotFoundException 但在 IntelliJ 中工作
【发布时间】:2017-12-21 12:20:19
【问题描述】:

我有一个 Kotlin Spring Boot 项目here。它有一些测试,从 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 之间来回切换。
  2. 将 Kotlin 运行时添加到 jar kotlinOptions.includeRuntime = true
  3. 将包级函数更改为对象中的函数。

我觉得很奇怪:

  1. 除了通常的 Gradle build 目录之外,还会创建一个 out 目录。
  2. 某些类/对象被编译为名称以Kt 结尾的类文件。我没有找到任何押韵或原因,但我还是 Kotlin 的新手,所以我可能会遗漏一些东西。

【问题讨论】:

    标签: spring spring-boot intellij-idea gradle kotlin


    【解决方案1】:

    您的测试(或实际测试的类)做了一些奇怪的事情:它们以某种方式删除了包含测试类的 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
    

    检查您的测试是否存在问题。

    【讨论】:

    • 你是对的。我的项目克隆了一个远程仓库然后构建它,在这个过程中它以某种方式删除了build 目录的内容。
    猜你喜欢
    • 2023-04-09
    • 1970-01-01
    • 1970-01-01
    • 2021-09-26
    • 2016-12-09
    • 2020-03-15
    • 1970-01-01
    • 2016-03-22
    • 1970-01-01
    相关资源
    最近更新 更多