Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ubuntu install Junit

Tags:

java

junit

ubuntu

I am trying to install Junit on Ubuntu. I have gotten Java to work so that I can compile java files. However, junit is giving me problems (cannot get the base test cases to work to prove installation worked correctly).

Here is what I did:

sudo apt-get install junit

Now when I type which junit it gives me /usr/bin/junit

Similarly, through another stack exchange post, I found that my junit.tar file is here:

/usr/share/java/junit.jar

I added the path to the junit.jar file to my CLASSPATH and since the website (http://junit.org/junit4/faq.html#started_2) says "make sure that the JUnit installation directory is on your CLASSPATH"

I added the second path above to my CLASSPATH in .bashrc

so when I run echo $CLASSPATH: I get /usr/share/java/junit.jar:/usr/bin

However, then when I try to run java org.junit.runner.JUnitCore org.junit.tests.AllTests

in order to test that I have installed and configured JUnit correctly, I get this error:

Error: Could not find or load main class org.junit.runner.JUnitCore 
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore

I am very much a beginner and am just learning Ubuntu, so any help would be greatly appreciated.

like image 906
bab354 Avatar asked Jun 16 '26 08:06

bab354


1 Answers

The problem is that you have installed JUnit 3.8 (see https://packages.ubuntu.com/artful/junit). And JUnit 3.8 doesn't contain any packages org.junit.*, as these were introduced in JUnit 4. In theory you could fix this by installing the package junit4 using apt-get (which will install JUnit 4.12).

But this is the wrong approach, you should learn how to use Maven or Gradle (or both), which will automatically download dependencies for you, and take away some of the chore of compiling (granted, in exchange for a bit higher learning curve).

Alternatively, if you really want to do this manually, get in the habit of downloading dependencies from project websites, and explicitly specifying the classpath during compilation and execution. Relying on system package managers for this is the wrong approach (wrong versions, different version requirements between applications/projects, libraries not in the repository, etc). Relying on the CLASSPATH environment variable is very brittle (wrong versions, unexpected effects of having libraries on the classpath etc), and as most ways of executing Java applications don't even use it, it is the wrong thing to learn.

TLDR, really, you should learn the basics of Maven or Gradle.

like image 100
Mark Rotteveel Avatar answered Jun 18 '26 22:06

Mark Rotteveel