用Kotlin编写单元测试时IntelliJ显示错误消息 [英] IntelliJ displays error message when unit test is written in Kotlin

查看:130
本文介绍了用Kotlin编写单元测试时IntelliJ显示错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有以下特征的项目

I have a project with following traits

  • IntelliJ Ultimate 2020.1
  • Java 13(带有module-info.java)
  • 科特林1.3.72
  • JUnit(+事实)
  • 行家(我相信这并不重要)

代码是混合的,有些类是用普通的Java编写的,另一些类是用Kotlin编写的,对于测试也是如此.一切都按预期进行,即

The code base is mixed, some classes are written using plain Java, others with Kotlin, the same is true for tests. Everything works as expected, that is

  • 所有代码均按正确的顺序编译,并且在Kotlin和Java之间可以完全互操作
  • 所有测试都可以使用mvn test或IntelliJ 运行测试"
  • 执行
  • 生成的jar可以运行(为了提供上下文)
  • all code is compiled in proper order and fully interoperable between Kotlin and Java
  • all test can be executed using either mvn test or IntelliJ "Run Test"
  • the resulting jar can be run (for the sake of providing context)

但是...

除了一切正常之外, IntelliJ警告,提醒我有关未声明的模块依赖项,仅当测试类使用科特林编写时.对于以纯Java 编写的测试类,此警告未显示.

apart from the fact that everything works, IntelliJ warns me about a non declared module dependency only if the test class is written in Kotlin. This warning is not displayed for test classes written in plain Java.

警告: 错误:(9,6)当前模块不依赖的模块'org.junit.jupiter.api'中声明了符号

该警告通常允许一个人导入/需要相应的模块/依赖项,但[alt]+[enter]对话框中没有提供解决方案.

That warning normally allows one to import / require the respective module / dependency, but there is no solution offered in [alt]+[enter] dialog.

到目前为止我尝试过的事情:

Things I have tried so far:

  • 从JUnit 4升级到5并没有改变情况
  • 无济于事:(
  • 通过使测试失败,确保在运行mvn test时确实执行了Kotlin编写的测试
  • 使用IntelliJ 运行测试"
  • 手动运行测试
  • 在/到Kotlin之间来回转换测试
  • 在模块信息中明确要求使用JUnit API/真相
  • upgrading from JUnit 4 to 5 didn't change the situation
  • googling to no avail :(
  • making sure tests written in Kotlin are really executed when mvn test is run by making a test fail
  • manually running test using IntelliJ "Run Test"
  • converting tests back and forth from / to Kotlin
  • explicitly requiring the the JUnit API / truth in module-info

后者显然可以阻止该警告,但它没有解决方案,因为它实际上产生了硬依赖性.根据我在谷歌搜索时发现的信息,maven-surefire-plugin确保包含测试依赖项.另外:如上所述,运行mvn test就像魅力一样,所以这似乎不是问题的一部分.

The latter obviously prevents the warning but is no solution since that actually produces a hard dependency. From what I found out while googling, the maven-surefire-plugin makes sure the test-dependencies are included. Also: running mvn test works like charm as stated above, so this does not seem to be part of the problem.

在编写测试时看到所有的红线实在令人遗憾...

Seeing all the red lines when writing test is really annyoing...

可疑行为

相同的测试,但是在Java中-一切都很好

same test but in java - everything is fine

如何解决IntelliJ中针对Kotlin测试类的警告?

How can I fix that warning for Kotlin Test Classes in IntelliJ?

  1. 我已经相信这是IntelliJ中的错误,但是很高兴向我展示我所忽略的内容.

  1. I have come to believe this is a bug in IntelliJ but I'd be happy to be shown what I overlooked.

由于从编译到运行Maven的所有过程都具有吸引力,因此我排除了有关项目结构等方面的细节.当前的问题与IntelliJ中的警告有关,而不是与损坏的构造或罐子不起作用有关.如果有必要,我会高兴地添加这些内容.

Since everything from compiling to running with Maven works like a charm, I excluded details regarding project structure and so on. The issue at hand is about the warning in the IntelliJ, not about a broken build or non-functional jars. I'll glady add those in case they turn out to be necessary.

而且,由于所有内容都有效(除了烦人的警告),我真的不知道该在哪里继续研究,因此提出了一个问题.

also since everthing actually works (apart from the annoying warning), I really don't know where to continue researching and hence created a question.

推荐答案

这是IDEA Kotlin插件错误突出显示的错误: https://youtrack.jetbrains.com/issue/KT-26037

This is a bug in IDEA Kotlin plugin error highlighting: https://youtrack.jetbrains.com/issue/KT-26037

解决方法:在软件包声明之前将@file:Suppress("JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE")添加到测试文件中.

Workaround: add @file:Suppress("JAVA_MODULE_DOES_NOT_DEPEND_ON_MODULE") to the test file before the package declaration.

这篇关于用Kotlin编写单元测试时IntelliJ显示错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆