“Installing” is a little misleading—since JUnit is a jar file, you aren’t really installing anything… you’re simply putting it somewhere.

The trick is to let java and your system know where to find JUnit.

So, let’s say you’ve downloaded JUnit 4.4 from http://junit.org. I downloaded the zip, so let’s assume you did that, too.

I put it in /Developer and ran:

unzip junit4.4.zip

Then, edit ~/.profile; you’ll need your path to know where junit is, and your classpath as well. My .profile wound up looking like this (only the $JUNIT_HOME stuff is really applicable, though you will definitely want to leave $PATH in there, so that you don’t get rid of everything that is in your path by default):

export JUNIT_HOME="/Developer/junit4.4" 
export PATH="/usr/local/bin:/usr/local/sbin:$PATH:$JUNIT_HOME" 
export CLASSPATH="$CLASSPATH:$JUNIT_HOME/junit-4.4.jar:$JUNIT_HOME" 

To get bash to know about the environment changes you just told it to make, you could close and restart your terminal, or you could type

source ~/.profile

Now, you should be able to type:

java org.junit.runner.JUnitCore org.junit.tests.AllTests

and see results something like:

JUnit version 4.4
..........................................................
..........................................................
..........................................................
..........................................................
.......................................I.II...............
..........................................
Time: 4.098

OK (329 tests)

Sorry, comments are closed for this article.