7

I'm new to the whole programming stuff but here's my problem:

I used to add my JUnit test cases in Eclipse by right clicking on the project, and just add New > JUnit Test Case.

Currently, I am not able to implement any test methods because Eclipse tells me on the line

import static org.junit.jupiter.api.Assertions.*;

the error message

The type org.junit.jupiter.api.Assertions is not accessible.

Error I get in the IDE:

enter image description here

I tried the following:

  1. Reinstalling Eclipse, using a fresh workplace.
  2. Adding the JUnit to Build path

Nothing helped.

It worked and works in older projects just fine.

Here is how the Package Explorer looks:

enter image description here

What am I missing?

howlger
  • 31,050
  • 11
  • 59
  • 99
jmuffin
  • 81
  • 1
  • 1
  • 6
  • What is the Java version? – Kris Feb 27 '20 at 10:53
  • Does this answer your question? [how to fix "The import org.junit.jupiter"?](https://stackoverflow.com/questions/51046506/how-to-fix-the-import-org-junit-jupiter) – Kris Feb 27 '20 at 10:55
  • I honestly don't understand the link you've posted - i've found that one too, i thought eclipse installs all the needed stuff for junit to work? (since it did before) Java is Version 8 u 221, eclipse version: 2019-09 JDK 15 – jmuffin Feb 27 '20 at 10:58
  • The JUnit library in build path must be JUnit5. From a partial look of your package explorer I think you have added JUnit 4 or 3. – Kris Feb 27 '20 at 11:00
  • 1
    You have a `module-info.java` file. Make sure you have the corresponding `requires` statement or consider deleting the `module-info.java` file. By the way, you are using an outdated Eclipse version. – howlger Feb 27 '20 at 11:01
  • 1
    oh wow, thank you so much. – jmuffin Feb 27 '20 at 11:06
  • Please [don’t post images of code, error messages, or other textual data.](https://meta.stackoverflow.com/questions/303812/discourage-screenshots-of-code-and-or-errors) – tripleee May 03 '23 at 06:19

2 Answers2

14

You use the Java Platform Module System (JPMS) by having a module-info.java file in the default package probably without the required requires <module>; statement. JPMS was introduced in Java 9.

Do one of the following:

  • Delete the module-info.java file (if needed, you can recreate it via right-clicking the project folder and choosing Configure > Create module-info.java)
  • In module-info.java add the corresponding requires statement, e.g. by going to the line with the import statement and using the corresponding Quick Fix (Ctrl+1)
howlger
  • 31,050
  • 11
  • 59
  • 99
0

I had this issue too on my Eclipse IDE on one computer, but it was not happening on another computer with fresh IDE installation. That was suspicious. I tried to export/import all settings, reimport all projects, even removing their IDE configuration files, nothing helped.

Then I compared plugins installed in Eclipse IDE on both computers and I have found some differences, so I uninstalled everything mentioning M2E, and then installed just basic M2E again (after restart).

The problem has gone.

Note that there is no "test module info" support in JPMS, so every tool has it's own solution.

dmatej
  • 1,518
  • 15
  • 24