Why does vscode not recognize the import org.junit?
Asked Answered
L

5

14

I am using maven to develop a java project within visual studio code, and am currently trying to write a test class. I have added junit as a dependency to my pom.xml file:

<dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
        <scope>test</scope>
</dependency>

Currently, my class looks like this: (there are no problems with the Board class, and getOne() returns 1)

import org.junit.Test;

public class BoardTest {
    private Board board = new Board();

    @Test
    public void testOne() {
        assert(board.getOne() == 1);
    }
}

Initially, when I open the file, everything is fine, but as soon as I save, vscode generates 2 error messages, both of which go away when I close and reopen the file, but appear again after a save:

  1. "The import org.junit cannot be resolved"

  2. "Test cannot be resolved to a type"

Interestingly, even with these errors present, vscode gives me mouseover information for both the import and the @Test flag, as if it has actually resolved them correctly. I have run mvn install from the command line, and vscode even lists junit-4.12.jar in the project's java dependencies section.

Running mvn test yields the expected result (the test passes), and after mvn package, running the project's .jar file from the command line runs the project with no problems. Whenever I try to run the project from vscode, it gives me a notice that the build has failed, even if the error messages are not currently present (i.e. after I have opened the test class but before I have saved). If I tell vscode to proceed anyway, the project again runs fine. Trying to run the test from vscode works the same way (I get an error message, but the test passes as normal after I tell vscode to proceed anyway).

Any ideas as to what could cause this? Here are the current versions of everything that I am using:

JDK: openjdk v11.0.7

vscode: v1.45.1

maven: Apache Maven v3.6.3

Leonhard answered 28/5, 2020 at 22:16 Comment(9)
You're importing junit as scope=test. This is correct, but means that it will only be visible in src/test/* directory. Where does your BoardTest file live?Objurgate
Also, what is the error message?Objurgate
I'm assuming you mean the one from when I try to run the test. It is the standard popup in the lower right that says "Build failed, do you want to continue?" By ignoring it, I mean I click the proceed option on this popup. The BoardTest class lives in src/test/java/display. display is just its package nameLeonhard
Check that you have junit-4.12.jar in maven .m2/repository folder. If not try to run 'mvn -B verify'Shocking
It sounds simply like a problem with VSCode IDE itself rather than any syntax errors. Try VSCode extension for maven for java: marketplace.visualstudio.com/…. Or use another IDERankin
junit-4.12.jar is in the location you suggested.Leonhard
Are you sure the test is actually running and passing via mvn test? If so, it's a IDE problem, try intellij or eclipse. If not, your pom or jar may be incorrect.Objurgate
I already have the maven extension installed, and the test is actually being run by mvn test (when I make it intentionally fail, the command reflects this) so I guess it is probably just a problem with vscode. Thanks!Leonhard
The problem goes away on eclipse, more evidence that it is a problem with vscodeLeonhard
S
36

Try this VS Code command...

View -> Command Palette -> Java: Clean Java Language Server Workspace

This command resolved my similar issue with VS Code not recognizing import org.json.* inside the .java files of a Gradle project even though Gradle would build and run successfully. This VS Code command is similar to IntelliJ's File -> Invalidate Caches and Restart in that it forces a refresh of the IDE's cache of available tokens and identifiers.

Side Notes:

  • Make sure to have run your build tool (e.g. gradle build or mvn install) at least once so that the required packages are downloaded to your local computer so VS Code can find them.
  • This command comes with the VS Code extension "Language Support for Java" from Red Hat (redhat.java) which comes bundled in the "Java Extension Pack" from Microsoft (vscjava.vscode-java-pack). You'll have to have one of these extensions installed to use it.
Shayn answered 13/4, 2021 at 17:31 Comment(5)
Sorry for taking so long to mark this as accepted! I have switched to using eclipse for this project, but I did open it up in vscode again to check your solution, and it fixed the problem. Thanks!Leonhard
You saved me!!!!. I havce been trying to solve this issue for ages. Almost quit.Genevagenevan
You broke my build!!! Now how can I fix it?Tuyere
Still a problem in 2023 and this is still the working solution :)Estrin
yes, reboot save everything.Blythe
H
1

I've done all the Java server cleaning and still getting '''package org.apache.lucene.analysis.tokenattributes does not exist(compiler.err.doesnt.exist)'''

build.gradle: 
    ext.pluginApiVersion = '8.12.2'
    ext.luceneVersion = '8.11.3'

    dependencies {
      implementation "org.apache.lucene:lucene- 
   core:${luceneVersion}"
      implementation "org.apache.lucene:lucene-analyzers- 
   common:${luceneVersion}"
      implementation 
    "org.elasticsearch:elasticsearch:${pluginApiVersion}"
      implementation 
    "org.elasticsearch.plugin:elasticsearch-plugin- 
   api:${pluginApiVersion}"
      implementation 
    "org.elasticsearch.plugin:elasticsearch-plugin-analysis- 
   api:${pluginApiVersion}"
    }
Houphouetboigny answered 29/2, 2024 at 18:34 Comment(2)
Certainly looks to me like you are asking a question rather than posting an answer. Can you explain how your answer resolves the problem described in the question?Euphorbia
This does not really answer the question. If you have a different question, you can ask it by clicking Ask Question. To get notified when this question gets new answers, you can follow this question. Once you have enough reputation, you can also add a bounty to draw more attention to this question. - From ReviewEuphorbia
A
1

In my case, the problem was that the unit tests were disabled in pom.xml.

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <maven.test.skip>true</maven.test.skip>
</properties>

If you have the properties like this, then comment the last line to enable the unit testing in VSCode.

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
    <!--  <maven.test.skip>true</maven.test.skip> -->
</properties>

You may implement something more fancy such as having enabling/disabling unit testing based on profiles to avoid this trouble.

Arita answered 4/3, 2024 at 20:4 Comment(0)
P
0

Setting maven user settings.xml path in vscode settings, run the above command View -> Command Palette -> Java: Clean Java Language Server Workspace and restarting vscode helped me fix the issue.

Philomena answered 23/7, 2021 at 14:28 Comment(0)
M
0

In my case, the problem was that my test class was not in the test folder, so the imports with test scope were not working.

Magdalenemagdalenian answered 15/7, 2024 at 11:52 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.