最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - JUnit not recognized in VS Code anymore after reopening project (no green triangle icons) - Stack Overflow

programmeradmin0浏览0评论

I'm trying to run JUnit tests in Visual Studio Code without using Maven or Gradle, just using plain .java files and .jar libraries.


Project Setup:

I'm using VS Code with the Java Extension Pack, and I added the following to .vscode/settings.json:

{
  "java.project.sourcePaths": [
    "src/main/java",
    "src/test/java"
  ],
  "java.project.outputPath": "bin",
  "java.project.referencedLibraries": [
    "lib/**/*.jar"
  ]
}

Inside the lib/ folder, I placed:

junit-4.13.2.jar

hamcrest-core-1.3.jarTestHello/
├── lib/
│   ├── junit-4.13.2.jar
│   └── hamcrest-core-1.3.jar
├── src/
│   ├── main/java/ExceptionTest/MessageUtil.java
│   └── test/java/ExceptionTest/TestJunit.java
├── .vscode/settings.json

What worked initially:

The first time I wrote my test, I just: Created .java files manually in a folder, Clicked on the Testing tab in the left sidebar, VS Code asked me to enable "Java Testing", and I accepted. It worked! I saw the green triangle "Run Test" icons next to my test methods.

The problem:

After closing and reopening VS Code later, now: When I click on the Testing panel, it says: "No tests found". The test annotations like @Test, @Before, and Assert are underlined with errors: The import .junit cannot be resolved, Test cannot be resolved to a type, Assert cannot be resolved. The green triangle icons (Run Test) have disappeared from the gutter

My test class (TestJunit.java):

package ExceptionTest;

import .junit.Test;
import static .junit.Assert.assertEquals;

public class TestJunit {

    String message = "Robert";
    MessageUtil messageUtil = new MessageUtil(message);

    @Test
    public void testSalutationMessage() {
        message = "Hi!" + "Robert";
        assertEquals(message, messageUtil.salutationMessage());
    }
}

Question: How can I make VS Code persistently recognize and run JUnit tests without Maven or Gradle? Why did the tests disappear after restarting VS Code? Is there a way to force VS Code to re-scan and show the test icons again?

I'm trying to run JUnit tests in Visual Studio Code without using Maven or Gradle, just using plain .java files and .jar libraries.


Project Setup:

I'm using VS Code with the Java Extension Pack, and I added the following to .vscode/settings.json:

{
  "java.project.sourcePaths": [
    "src/main/java",
    "src/test/java"
  ],
  "java.project.outputPath": "bin",
  "java.project.referencedLibraries": [
    "lib/**/*.jar"
  ]
}

Inside the lib/ folder, I placed:

junit-4.13.2.jar

hamcrest-core-1.3.jarTestHello/
├── lib/
│   ├── junit-4.13.2.jar
│   └── hamcrest-core-1.3.jar
├── src/
│   ├── main/java/ExceptionTest/MessageUtil.java
│   └── test/java/ExceptionTest/TestJunit.java
├── .vscode/settings.json

What worked initially:

The first time I wrote my test, I just: Created .java files manually in a folder, Clicked on the Testing tab in the left sidebar, VS Code asked me to enable "Java Testing", and I accepted. It worked! I saw the green triangle "Run Test" icons next to my test methods.

The problem:

After closing and reopening VS Code later, now: When I click on the Testing panel, it says: "No tests found". The test annotations like @Test, @Before, and Assert are underlined with errors: The import .junit cannot be resolved, Test cannot be resolved to a type, Assert cannot be resolved. The green triangle icons (Run Test) have disappeared from the gutter

My test class (TestJunit.java):

package ExceptionTest;

import .junit.Test;
import static .junit.Assert.assertEquals;

public class TestJunit {

    String message = "Robert";
    MessageUtil messageUtil = new MessageUtil(message);

    @Test
    public void testSalutationMessage() {
        message = "Hi!" + "Robert";
        assertEquals(message, messageUtil.salutationMessage());
    }
}

Question: How can I make VS Code persistently recognize and run JUnit tests without Maven or Gradle? Why did the tests disappear after restarting VS Code? Is there a way to force VS Code to re-scan and show the test icons again?

Share Improve this question edited yesterday Crescenzo De Marco asked yesterday Crescenzo De MarcoCrescenzo De Marco 11 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

I tested your project and there is no problem. This may be caused by cache. To re-scan, click Java Projects panel -> three dots (more action) -> clean workspace. Then prompt a warning message window -> reload and delete.

发布评论

评论列表(0)

  1. 暂无评论