【问题标题】:JUnit with Kotlin - This class does not have a constructor带有 Kotlin 的 JUnit - 此类没有构造函数
【发布时间】:2023-03-14 22:43:01
【问题描述】:

我正在尝试在我的 Kotlin 应用程序中使用 JUnit4 实现测试(因为 kotlin.test 在我的 Kotlin 运行时库中似乎不存在,我现在不知道如何获取它)。
但是,我只是通过使用 Test 注释遇到了错误。
这是我的代码:

import junit.framework.*

class IntTest {
    @Test    <------------- This line
    fun test1() {

    }
}

在指定的行,Eclipse 给出错误消息:"This class does not have a constructor"
我不明白有什么问题。哪个类没有构造函数,为什么要有构造函数?

【问题讨论】:

  • 尝试添加import org.junit.Test;

标签: java testing junit kotlin


【解决方案1】:

junit.framework.Test 是一个接口,它的实现自 API 24 以来一直是deprecated。由于它是一个接口,因此它会导致错误

"This class does not have a constructor".

在实际寻找类的注解处理期间。

解决方案:删除它并使用import org.junit.Test;

【讨论】:

    【解决方案2】:

    这个问题有点老了。但我最近遇到了同样的问题。 我在 Intellij Idea 中遇到了同样的错误。

    我使用的构建工具是 gradle。为了设置测试依赖项,我按照 kotlinlang 网站上的说明进行操作:Test code using Junit in JVM

    测试运行良好,但在 IDE 中我看到了同样的错误。

    我可以通过更改以下依赖项来解决此问题:

    testImplementation 'org.jetbrains.kotlin:kotlin-test:1.5.21'
    

    到:

    testImplementation 'org.jetbrains.kotlin:kotlin-test-junit5:1.5.21'
    

    这实际上添加了 Junit 5 依赖项。我认为这是必要的,因为您可能必须选择要用于 kotlin.test 的底层测试框架。

    编辑:在我的测试中,我像教程中一样导入import kotlin.test.Test

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-13
      • 1970-01-01
      • 1970-01-01
      • 2019-11-15
      • 1970-01-01
      • 2019-06-05
      • 1970-01-01
      相关资源
      最近更新 更多