Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The import org.junit cannot be resolved (VSCode) #926

Closed
cybersinger opened this issue Dec 19, 2020 · 6 comments
Closed

The import org.junit cannot be resolved (VSCode) #926

cybersinger opened this issue Dec 19, 2020 · 6 comments

Comments

@cybersinger
Copy link

cybersinger commented Dec 19, 2020

I can't use JUnit test framework with VSCode, get message "The import org.junit cannot be resolved". I tried:

  1. Installed all recommended Java extension and then some more.
  2. Downloaded junit-platform-console-standalone-1.7.0-M1 jar file and pointed settings.json to it.
  3. Created new Java project through Project Manager for Java extension.
  4. Consulted documentation on junit.org, unfortunately, I couldn't make much of it, as instructions use terms that I am not familiar with.
Environment
  • Operating System: Windows 10 Pro (10.0.19042)
  • JDK version: AdoptOpenJDK 11.0.9.1-hotspot
  • Visual Studio Code version: 1.52.1
  • Java extension version: 0.12.1
  • Java Debugger extension version: 0.1.5
Steps To Reproduce
  1. created simplest possible program with @test line.
package prvenac;

import static org.junit.Assert.assertTrue;
import org.junit.Test;
public class AppTest 
{
    @Test
    public void shouldAnswerWithTrue()
    {
        assertTrue( true );
    }
}

Full "project" available here: https://github.com/cybersinke/programi
(I am new to GitHub and I probably didn't document this issue properly. I uploaded the files manually.)

Current Result

The import org.junit cannot be resolved.

Expected Result

My understanding is that the extensions I installed should enable very simple use of junit, i.e. my code above should work?

Additional Informations
@jdneo
Copy link
Member

jdneo commented Dec 21, 2020

The JUnit dependency defined in pom.xml is JUnit 4, and the junit-platform-console-standalone-1.7.0-M1.jar is for JUnit 5. If you want to play with JUnit 4, I think there is no need to download other jars given your pom configuration

@cybersinger
Copy link
Author

Thanks for comment jdneo! I don't mind which release it would be, so I downloaded junit-4.11 to a folder included in Windows "Environmental Variables". Meanwhile I reset "settings.json" file, and don't know whether I should specify the path to junit .jar there, and if yes how to do it? Restarted VSCode, but it still doesn't work. Can you please bear with me, solution is probably very simple, but I spent hours searching for it.

@jdneo
Copy link
Member

jdneo commented Dec 22, 2020

Ok, so I suppose you are not using the Maven as the project build tool. But just want to try some test features in a very simple folder. Here is what you can do. (taking the Junit 5 as an example)

  1. Create a new Java Project using Project Manager for Java extension
    image

  2. Copy the jar file junit-platform-console-standalone-1.7.0-M1.jar into the lib folder
    image

Then you can start to write your test cases.

Note: Basically, there is no need to do some other configurations because the Java Language Server will by default consume the jars in the lib folder as the project dependency for the project without any build tools.

@no-response
Copy link

no-response bot commented Feb 3, 2021

This issue has been closed automatically because it needs more information and has not had recent activity. Please reach out if you have or find the answers we need so that we can investigate further.

@no-response no-response bot closed this as completed Feb 3, 2021
@ca-jimen
Copy link

The issue, I believe, is that you need to make sure the file is under the test folder.

@Odusseus
Copy link

Odusseus commented Jan 4, 2024

Yes, its work for me. Thank you. Pascal
pom.xml

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-test</artifactId>
	<scope>test</scope>
</dependency>

Java

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

import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class UtilsTests {

@test
public void trueTest(){
assertTrue( true );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants