IntelliJ displays error message when unit test is written in Kotlin

纵饮孤独 提交于 2020-12-13 03:05:34

问题


Context

I have a project with following traits

  • IntelliJ Ultimate 2020.1
  • Java 13 (with module-info.java)
  • Kotlin 1.3.72
  • JUnit (+ truth)
  • maven (I believe this to be unimportant)

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

  • 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)

but...

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.

The warning: Error:(9, 6) Symbol is declared in module 'org.junit.jupiter.api' which current module does not depend on

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:

  • 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

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...

the suspect behavior

same test but in java - everything is fine

Question:

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

Note

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

  2. 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.

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


回答1:


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

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



来源:https://stackoverflow.com/questions/61321472/intellij-displays-error-message-when-unit-test-is-written-in-kotlin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!