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

How to use JUnit in VSCode #658

Closed
ZHANG-Quentin opened this issue Mar 18, 2019 · 25 comments
Closed

How to use JUnit in VSCode #658

ZHANG-Quentin opened this issue Mar 18, 2019 · 25 comments

Comments

@ZHANG-Quentin
Copy link

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.

@jdneo
Copy link
Member

jdneo commented Mar 18, 2019

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

@ZHANG-Quentin
Copy link
Author

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.

@jdneo
Copy link
Member

jdneo commented Mar 19, 2019

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

@ZHANG-Quentin
Copy link
Author

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.

@SupinePandora43
Copy link

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

@jdneo
Copy link
Member

jdneo commented Mar 24, 2019

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.

@jdneo
Copy link
Member

jdneo commented Mar 26, 2019

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

@niccolomineo
Copy link

niccolomineo commented Jun 21, 2019

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?

@SupinePandora43
Copy link

@niccolomineo

demo

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

(yourJavaFile and lib folder in one folder)

@niccolomineo
Copy link

niccolomineo commented Jun 21, 2019

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.

@jdneo
Copy link
Member

jdneo commented Jun 22, 2019

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.

@niccolomineo
Copy link

niccolomineo commented Jun 22, 2019

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.

@SupinePandora43
Copy link

@niccomineo try to open battleship folder as workspace

@niccolomineo
Copy link

niccolomineo commented Jun 22, 2019

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.

@SupinePandora43
Copy link

SupinePandora43 commented Jun 22, 2019

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)

@niccolomineo
Copy link

niccolomineo commented Jun 22, 2019

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 /src are. Where should they go and what should be in them?
  • what module should I import in each .java file?

@SupinePandora43
Copy link

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

@niccolomineo
Copy link

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.

@SupinePandora43
Copy link

@niccolomineo wait ~5 minutes

@SupinePandora43
Copy link

@niccolomineo
dummy.zip

@niccolomineo
Copy link

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).

@jdneo
Copy link
Member

jdneo commented Jun 23, 2019

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

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

@edelauna
Copy link

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"/>

@jdneo
Copy link
Member

jdneo commented Jan 30, 2020

@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

@MrYakobo
Copy link

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

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

6 participants