15

I am not able to figure out how to correctly install JUnit onto my mac. I know I am supposed to add it to the path environmental variable, and I have tried a few tutorials I've found on Google on how to do that, but I keep getting errors. This is the link to the tutorial I have used: http://hathaway.cc/post/69201163472/how-to-edit-your-path-environment-variables-on-mac-os-x I have a feeling I am doing something wrong in step 3. I have placed the junit.jar file in the Library folder by the way.

Any help will be greatly appreciated!

user3238564
  • 151
  • 1
  • 1
  • 3

5 Answers5

39

Preliminary checks: first check your JRE is installed ok. You should be able to open a terminal and type javac without getting any errors, and see the usage options:

Usage: javac <options> <source files>
where possible options include:
...

Also typing whereis javac will give you the path of where your java compiler is installed. When I type this on my OSX installation, I get /usr/bin/javac. You should get some path shown to you with no errors. Assuming that all checks out, let's install jUnit. Follow the steps below.

  1. Download the .jar files. Go to https://github.com/junit-team/junit/wiki/Download-and-Install and click the link for junit.jar. Find the row for the latest stable release then under the downloads column click 'jar' link. This will download a file junit-X.Y.jar. Repeat this again and download the latest stable release for hamcrest: download hamcrest-core-XX.YY.jar
  2. Create a jUnit Home folder. We need to create a folder and put the .jars you downloaded into this folder. I suggest creating a java folder in your home directory by running cd && mkdir java. Then running cp ~/Downloads/{junit-X.Y.jar,hamcrest-core-XX.YY.jar} ~/java/ to copy the two .jars there. (replace X, Y, XX, YY accordingly). [Note: If the folder placement becomes an inconvenience, don't worry, you can change the jUnit folder at any later date].
  3. Edit your classpath. We now need to edit our .bash_profile file to add these files to our classpath (if you are using zsh edit your .zshrc file).

    export JUNIT_HOME="$HOME/java"
    export PATH="$PATH:$JUNIT_HOME"
    export CLASSPATH="$CLASSPATH:$JUNIT_HOME/junit-X.Y.jar:$JUNIT_HOME/hamcrest-core-XX.YY.jar"
    
  4. Test it works. Restart your terminal. Run echo $CLASSPATH, there should be no errors that complain that files are unable to be found. Now create a test file with a trivial test case. In your java folder, create a file called TestBasic.java with:

    import junit.framework.TestCase;
    public class TestBasic extends TestCase {
      public void testTrue() {
        assertTrue(true);
      }
    }
    

Now cd into the java directory run javac TestBasic.java followed by java org.junit.runner.JUnitCore TestBasic. If everything is ok, then you will get an output like:

JUnit version 4.11
.
Time: 0.006

OK (1 test)
James Lawson
  • 8,150
  • 48
  • 47
3

Creating a .bash_profile did not work for me, but the following commands did, after following steps 1 and 2 of James's Lawson's post above.

javac -cp .:junit-X.Y.jar TestBasic.java

followed by

java -cp .:junit-4.XX.jar:hamcrest-core-XX.YY.jar org.junit.runner.JUnitCore TestBasic
  • This worked for me: javac -cp .:junit-X.Y.jar TestBasic.java followed by: java -cp .:junit-4.10.jar:org.junit.runner.JUnitCore org.junit.runner.JUnitCore TestBasic – JayS Sep 21 '18 at 15:21
0

JUnit doesn't have to be installed. It's a Java library, like all the other libraries, that you can use in your code. The PATH must not be changed to use it. You just need it in the classpath, like all the other Java libraries:

javac -cp junit.jar ...

to compile, and

java -cp junit.jar ...

to run.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • The code snippets you've provided aren't usable as-is. In particular, the ellipses don't tell me what to put in there. And it doesn't look like javac with a JAR argument will build anything, since the jar is already built. – davidgoli Nov 07 '14 at 19:09
  • In particular, if you're using Ant or Maven to run, the `java -cp` flags won't apply. A good answer should involve permanently adding the JAR to the classpath. – davidgoli Nov 07 '14 at 19:15
  • @davidgoli: this answer is not meant to answer your question, which is unknown to me, and different from the one the OP asked. If you have a question, ask it. Don't complain about an answer to a different, unrelated question. There is no such thing as a "permanent" classpath anyway. ANd the goal is not to build the junit jar. The goal is to compile code which depends on JUnit. – JB Nizet Nov 07 '14 at 19:24
  • 1
    The OP absolutely did ask how to "install" junit, not "compile" with junit. – davidgoli Nov 07 '14 at 20:38
  • 2
    And the point of the answer is: there is nothing to install. You get the jar file, put it somewhere, and refer to it in the classpath when compiling or running your code. – JB Nizet Nov 08 '14 at 07:21
0
  1. Unzip the downloaded junit file "junit4.11" (whichever version you are using)
  2. Open terminal
  3. Type command:

    open .bash_profile

  4. A textfile will be opened: Paste this command in it

    CLASSPATH=$CLASSPATH:"path where junit4.11 is saved"/\junit4.11/junit-4.11.jar:"path where junit4.11 is saved"/\junit4.11/junit-dep-4.11.jar export CLASSPATH

save the textfile and close it.

  1. Come back to the terminal and type commands:

    cd /"path where junit4.11 is saved"/junit4.11 java org.junit.runner.JUnitCore org.junit.tests.AllTests

You should see the success message.

lbusett
  • 5,801
  • 2
  • 24
  • 47
tech11
  • 27
  • 2
  • 7
0

I did not have to add it to path. I downloaded the file and added it manually to my libraries.

If you are using IntelliJ IDEA go to the root folder of your project > Open Modules and Settings > Libraries > New Project Library > Java > and select where your junit file is located.

sptramp
  • 897
  • 2
  • 10
  • 29