NetBeans 10 JUnit Jar not found
Asked Answered
I

4

9

I have a new installation of NetBeans 10. Trying to run some initial unit tests I just created, I get the following error:

The <classpath> or <modulepath> for <junit> must include junit.jar if not in Ant's own classpath

I could probably hack the build script to include junit.jar, but I want to know: what's the right way to fix this?

Shouldn't NetBeans come with a version of JUnit already accessible? Should I configure my project differently? How do I add a path to the library?

How can I find the classpath for Ant (and what version/binary NetBeans is using)?

The project Test Libraries shows that JUnit 5.3.1 is present, I have three Jar files listed there: junit-jipiter-api, junit-jupiter-params, junit-jupiter-engine. But it seems to be not actually found.

The project is a standard Java Library (no main class). I didn't add any "extras" or mess with the default project setup that NetBeans uses. Just used the basic setup wizard thing.


Re. a reply from the NetBeans mailing list by Geertjan Wielenga, he pointed me at this thread and reply:

Yep...

We never got around to implementing JUnit 5 support for Ant based projects in NB 10.

John McDonnell

http://mail-archives.apache.org/mod_mbox/netbeans-users/201901.mbox/%3cCAAuDyk6WcgLaUDz0dp=4Arr7QGnwWcYey3VOeGojRRMRqVZrFA@mail.gmail.com%3e

So I think it's just not going to work. I'll try the suggested reversion to JUnit 4 below.

Indium answered 12/1, 2019 at 16:44 Comment(0)
L
17

I'm running netbeans 10 as well, in xubuntu if that helps. I have no idea what is going on, so I had to revert to junit4. EDIT: To be perfectly clear, ant needs junit.jar which is nowhere to be found in the default netbeans 10 installation. Until someone brings a better solution, revert to junit4:

1) in your test libraries, right click and import the junit 4.1.2 ones.

2) remove the junit 5.3 ones.

3) scrap all the imports in your test file and use the junit4 ones. ie your imports will look like:

import static junit.framework.Assert.assertEquals;
import org.junit.Test;
import org.junit.*;
/*import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;*/

4) scrap the beforeAll and other test tags. Only leave the @Test one.

5) save all, close and re-open the project. It will complain that the hamcrest libraries for junit4 are not imported. Allow netbeans to fix it for you.

With that done, I was able to test successful and failing tests. I guess when you generate the junit tests with the template generator, it will generate the junit5 ones, so, a bit annoying. But you'll have tests!

Lissy answered 12/1, 2019 at 19:0 Comment(6)
Thanks for that idea, if I don't hear anything back from the NB mailing list I'll probably do that. FYI the JUnit 5 test libraries (in Windows) are under incubating-netbeans-10.0-bin/netbeans/platform/modules/ext/Indium
This did work for me. Thanks for the clear description how to fix this.Indium
Sadly Netbeans 11 also presents the issue, but solution works as wellMalek
Still the case with NetBeans 11.2 as of today. Thank you very much for the fix.Shih
And in Netbeans 13 too!Donative
Still the case in Netbeans 18Lange
H
0

I had the same error but it was not a configuration issue, it was only a typo.

The main method of the class I was testing was written like this:

public static void main(String agrs) {
 ///
}

The only thing I had to do was to write it properly, like this:

public static void main(String[] args) {
 ///
}

Once I changed it, the problem was gone. I suppose this is not a global solution, but may help some.

Herat answered 14/6, 2020 at 1:57 Comment(0)
F
0

I found a better solution to the accepted answer is to select JUnit 4 from Create/Update tests. Then you don't have to reconfigure your tests every time.

From Netbeans: https://netbeans.apache.org//kb/docs/java/junit-intro.html#_writing_junit_3_unit_tests

Ferrule answered 8/3, 2021 at 9:52 Comment(1)
Thanks for that suggestion. I'll test this out next time I get a chance.Indium
S
0

I faced the same problem. I removed the existing Junit 5 files and added the files here instead.

https://github.com/junit-team/junit4/wiki/Download-and-Install

Semiprofessional answered 10/5, 2022 at 9:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.