r/javahelp • u/TealedLeaf • Sep 21 '26
Homework Struggling to get JUnit tests to work in VSCode
Prof really seemed to like VSCode and sung its praises, but all of our notes are for IntelliJ and I can't figure out what I'm doing wrong.
In IntelliJ we're using Maven. It sounded like all I had to do was hit a few buttons and it'd set me up. In the Testing tab I currently have the initial folder > { } <Default Package> > ClassTest
@Test underlines red. I thought it was something I needed to install from Extensions, but I have the Language Support for Java, Extension Pack for Java, Maven for Java, and from what I've found that's literally all I need?
I got my test file from the Command Pallet Java: Go to Test
I have been fighting VSCode for 3 hours today for every step.
I'm obviously missing something, but I don't know what it is, I'm frustrated, and atp I don't understand why this was even recommended with no notes and most of the class doing it through IntelliJ. Advice??? Sorry this is probably a stupid question/place to be stuck with, I just don't know what to do.
•
u/AutoModerator Sep 21 '26
Please ensure that:
You demonstrate effort in solving your question/problem - plain posting your assignments is forbidden (and such posts will be removed) as is asking for or giving solutions.
Trying to solve problems on your own is a very important skill. Also, see Learn to help yourself in the sidebar
If any of the above points is not met, your post can and will be removed without further warning.
Code is to be formatted as code block (old reddit: empty line before the code, each code line indented by 4 spaces, new reddit: https://i.imgur.com/EJ7tqek.png) or linked via an external code hoster, like pastebin.com, github gist, github, bitbucket, gitlab, etc.
Please, do not use triple backticks (```) as they will only render properly on new reddit, not on old reddit.
Code blocks look like this:
You do not need to repost unless your post has been removed by a moderator. Just use the edit function of reddit to make sure your post complies with the above.
If your post has remained in violation of these rules for a prolonged period of time (at least an hour), a moderator may remove it at their discretion. In this case, they will comment with an explanation on why it has been removed, and you will be required to resubmit the entire post following the proper procedures.
To potential helpers
Please, do not help if any of the above points are not met, rather report the post. We are trying to improve the quality of posts here. In helping people who can't be bothered to comply with the above points, you are doing the community a disservice.
I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.
5
u/LetUsSpeakFreely Sep 21 '26
The IDE is irrelevant, VSCode, intellij, eclipse, it doesn't matter. So long as your package structure is correct (src/test/path/to/class/ClassTest.java), you have the testing libraries included in your pom.xml, a maven target to run your tests, and your test methods annotated with @Test, it will work.
There are several plugins for VSCode to facilitate testing, but you can run your tests suite using the mvn <target> command from three terminal.
2
u/KAJed Sep 21 '26
Hey devs: Stop suggesting different IDEs and answer the question proposed. There’s no reason a correctly configured pom and tests shouldn’t work in vscode.
That being said: I found better success using newer version of junit. I found vscode got confused with old ones.
3
u/perryplatt Sep 21 '26
Do you have Junit as a dependency in your pom file. Can you post your pom.xml and settings.json?
0
u/_a_beak_ Sep 21 '26
For java, just use intellij. These are just ides, intelligent notepads. Nobkdy cares where you write code.
1
u/Kautsu-Gamer Sep 22 '26
First, you must run install after chamges to load dependencies. Took a while to figure out.
After every change in POM run maven install with Maven extension.
1
u/jlanawalt Sep 23 '26
Sorry to hear you’re having trouble. That can be frustrating, especially when it eats up hours.
As others have currently hinted, using a tool like Maven makes your projects less IDE dependent.
I want to recommend you follow some simple guide on using JUnit in Maven, but you’ve had success with Maven projects in IntelliJ, so make one in that and look at it, study the layout, read the pom.xml, see why it works, and then try running mvn test outside IntelliJ. That will probably require more “yak shaving” to install maven, so your call.
A proper maven project should be openable and runnable in any maven-supporting IDE
-1
u/DemicideMMMCCCI Sep 21 '26
Might not answer your question but, why aren't you using IntelliJ?