How to install JUnit 4 on MacOS Catalina

Posted By Aung Nay on Nov 22, 2020


Step 1: Verify Java Installation

Open Terminal check Java version.

java -version

You should get an output similar to this.

java version "14.0.2" 2020-07-14
Java(TM) SE Runtime Environment (build 14.0.2+12-46)
Java HotSpot(TM) 64-Bit Server VM (build 14.0.2+12-46, mixed mode, sharing)

If you do not have Java on your mac, install JDK from

Azul JDK

https://www.azul.com/downloads/zulu-community/?package=jdk

Open JDK 15

https://jdk.java.net/15/

Oracle JDK

https://www.oracle.com/java/technologies/javase-downloads.html

Install using instructions from respective sites.

Step 2: Set JAVA_HOME

Using Finder Go to Folder, go to /Library/Java/JavaVirtualMachines/ and verify that /jdk-XX.XX.XX.jdk/Contents/Home/bin/ folder exist and that there are Unix executables including java and javac. (XX means numbers based on your Java installation.)

In Terminal,

export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk-XX.XX.XX.jdk/Contents/Home

(Make sure you replace XX with the actual numbers from what you see in Finder.)

Verify Java install by using java -version command again.

Restart Terminal and test by entering

echo $JAVA_HOME

Your output should show the path you just set.

Step 3: Download JUnit 4

Go to https://github.com/junit-team/junit4/wiki/Download-and-Install and download junit.jar and hamcrest-core.jar. The file names should be junit-X.XX.X.jar and hamcrest-core-X.X.jar. (XX means numbers.)

Create a folder name JUNIT in your Home using Finder. Move both files from Downloads to JUNIT folder.

Step 4: Setting JUNIT_HOME and CLASSPATH

Catalina uses zsh, so edit your .zshrc file (It’s a hidden file). Go to your Home folder using Finder. Hold down Cmd + Shift + “.” (That’s the dot). Finder will now show you all the hidden files.

If .zshrc file does not exist, using Terminal to create .zshrc file by typing in touch ~/.zshrc

Open .zshrc file with a text editor, paste the following commands and save it.

export JUNIT_HOME="$HOME/JUNIT"
export PATH="$PATH:$JUNIT_HOME"
export CLASSPATH="$CLASSPATH:$JUNIT_HOME/junit-X.XX.X.jar:$JUNIT_HOME/hamcrest-core-X.X.jar"

(Don’t forget to replace the XXs with numbers.)

Now, restart your Terminal and test the following:

echo $JUNIT_HOME

The output should show the path you just set.

echo $CLASSPATH

The output should show the path you just set for both files.

Step 5: Test JUnit Setup

Follow Step 6 and 7 from https://www.tutorialspoint.com/junit/junit_environment_setup.htm

Reference:

Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This