Vscode-java-test: How to use JUnit in VSCode

Created on 18 Mar 2019  路  25Comments  路  Source: microsoft/vscode-java-test

It's a question from beginners. I use JUnit in eclipse and IntelliJ. Both of them are designed for JAVA so it's not difficult to find tutorials about setting JUnit environment. But I cann't find any detailed solution on github or stackoverflow. When I insert the import static org.junit.Assert.*; at the front of the file, there's an error The import org.junit cannot be resolved.

I've downloaded JUnit.jar, how to import it into VSCode and configure the classpath of it? Thank you.

duplicate

Most helpful comment

@niccolomineo

demo

you need:
workspaceFolder ->
/lib -> junit.jar
/yourClass.java

(yourJavaFile and lib folder in one folder)

All 25 comments

Hi @Yi-yi,

Thank you for trying out our extension. May I ask what kind of project is it?

  • Is it a Maven project?
  • Or is it a Gradle project?
  • Or is it an Eclipse/IntelliJ project?

In other words, how do you manage your dependencies within your project, like JUnit?

// cc @yaohaizh

Hi @Yi-yi,

Thank you for trying out our extension. May I ask what kind of project is it?

* Is it a Maven project?

* Or is it a Gradle project?

* Or is it an Eclipse/IntelliJ project?

In other words, how do you manage your dependencies within your project, like JUnit?

// cc @yaohaizh

Sorry I'm not sure what kind of project it is. It's the java file I create originally/directly from VSCode.

Hmm, if you don't mind, is it possible for you to share your project with us?

JunitVSCode.zip
It's a 'HelloWorld' project I create to test/practise Junit in VSCode. In the AppTest.java, errors: The import org.junit cannot be resolved

Should I modify/configure the files of '.classpath' and '.project'? Or, import the 'junit.jar' into the project?

Thank you.

i'm recommend you use gradle project. its a easy way to run test's, just include junit in build.gradle

Thank you @SupinePandora43.

@Yi-yi Yes, As @SupinePandora43 has said, for now, using the build tool: Maven/Gradle could be a workaround to solve your problem.

Closing since this issue is duplicated with #348

@Yi-yi Sorry that the extension does not support the project without the build tools(Maven/Gradle). For now, you can try to add Maven/Gradle into the project, or wait for us resolving #348

Hi, I have the same issue. How to sort this out without Maven/Gradle? I put the junit.jar file in a /lib directory at the same level as my proejct's /bin and /src, but to no avail. If that's correct, I understand I have to set up the classpath. But how to do that?

@niccolomineo

demo

you need:
workspaceFolder ->
/lib -> junit.jar
/yourClass.java

(yourJavaFile and lib folder in one folder)

Hi, thanks, done that, but I am still getting "The import org.junit cannot be resolved". I assume it's a PATH problem but I don't know how to configure it dynamically for each project.

Thank you for the help @SupinePandora43.

@niccolomineo If you still have the classpath problem, that might be related with the Java Language Server. Could you please try to trigger the command Java: Clean the Java Language Server workspace > Restart and delete.

Thank you for the help @SupinePandora43.

@niccolomineo If you still have the classpath problem, that might be related with the Java Language Server. Could you please try to trigger the command Java: Clean the Java Language Server workspace > Restart and delete.

Thank you jdneo, unfortunately doing so doesn't have any effect.

So, ruling out the directory structure problem

Schermata 2019-06-22 alle 10 17 57

I have the following questions:

  • Do I need to add anything to my Bash Profile?
  • Is the junit.jar filename expected to be in a certain way?
  • Do I need to change anything in VSCode's configuration?
  • Do I need any other plugin besides Java Test Runner?

Thank you.

@niccomineo try to open battleship folder as workspace

Ok, that seems to get rid of the JUnit-related errors, thank you! Now I get this message, though:

Schermata 2019-06-22 alle 10 56 45

Also, when I click on the play arrow in Test Explorer nothing happens.

try to move all your files/folders in workspace to previous workspace folder

Schermata 2019-06-22 alle 10 17 57

for structure like:
workspace
| .vscode
| lib
.. | junit.jar
| BattleCruiser.java
| OceanTest.java
(".." for indent)
(not in src folder)

Ok, I put the .vscode folder in the src/battleship folder. The latter is now my workspace folder.

  • What should be in launch.json?
  • Also, I have a .classpath and .project files where /bin and /srcare. Where should they go and what should be in them?
  • what module should I import in each .java file?

@niccolomineo NO, i tryed say to don't use src/battleship folder!

Can you please attach a dummy project so that I can see what it should be like? It should be able not only to run tests but also to compile java classes. Thanks.

@niccolomineo wait ~5 minutes

@niccolomineo
dummy.zip

Thanks, it works now and I'd suggest to somehow add this dummy project to the docs.
In addition, all those having issues may install Eclipse and create a workspace/project to check how to include all paths properly (particularly those in and related with .classpath).

Thank you @SupinePandora43 again for your great work and contribution! Well done!! 馃憤

@niccolomineo Thank you for your advice, we will consider it!

JunitVSCode.zip
It's a 'HelloWorld' project I create to test/practise Junit in VSCode. In the AppTest.java, errors: The import org.junit cannot be resolved

Should I modify/configure the files of '.classpath' and '.project'? Or, import the 'junit.jar' into the project?

Thank you.

Adding this line to the .classpath folder worked for me:

<classpathentry kind="con" path="org.eclipse.jdt.junit.JUNIT_CONTAINER/4"/>

@edelauna It's not recommended to modify the .classpath file. Instead, you can put the jars into the lib folder.

https://code.visualstudio.com/docs/java/java-project#_adding-external-jar

Just for clarity, here is the file structure from the dummy zip by @niccolomineo, if someone don't want to download a zip file

dummy

Was this page helpful?
0 / 5 - 0 ratings