Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need help installing JUnit on Mac/How to add JUnit to Path environmental variable on Mac OSX

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!

like image 635
user3238564 Avatar asked Jan 26 '14 22:01

user3238564


People also ask

How do I download JUnit from terminal?

Method 2: By Downloading JUnit zip file Step 1: Open up the terminal and run the following command to download the JUnit. zip. Step 2: Move this junit file to another folder called JUnit and unzipping the file. Now, make sure you are in Download/JUnit folder.


1 Answers

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)
like image 178
James Lawson Avatar answered Sep 20 '22 15:09

James Lawson