Skip to content

Instantly share code, notes, and snippets.

@xjcoan
Last active January 25, 2024 20:24
Show Gist options
  • Save xjcoan/d57cca33f3b046acbb5b25a4bd2d6760 to your computer and use it in GitHub Desktop.
Save xjcoan/d57cca33f3b046acbb5b25a4bd2d6760 to your computer and use it in GitHub Desktop.
Install JUnit testing on a Mac environment (written for JUnit 4)
Download the latest version of JUnit here : <https://github.com/junit-team/junit4/wiki/Download-and-Install>
(I place mine in ~/java/ for this example)
Add the following lines to terminal conf. file (~/.zshrc)
`export JUNIT_HOME="$HOME/java"`
`export PATH="$PATH:$JUNIT_HOME"`
`export CLASSPATH="$CLASSPATH:$JUNIT_HOME/junit-4.12.jar:$JUNIT_HOME/hamcrest-core-1.3.jar"`
Alias junit in zshrc: `alias junit="java org.junit.runner.JUnitCore"`
You can now run JUnit tests: `junit YourTestClass`
@Ibrahim004
Copy link

Hi,

Thanks for the setup instructions. I am trying to run JUnit tests from the terminal on Mac. I am getting this error:
Error: Could not find or load main class org.junit.runner.JUnitCore
Caused by: java.lang.ClassNotFoundException: org.junit.runner.JUnitCore

Do you know how I should solve this? I added the junit and hamcrest jars to ~/java directory. Thanks again!

@xjcoan
Copy link
Author

xjcoan commented May 24, 2021

@Ibrahim004
Hey, thanks for looking at this gist. It uses currently out-of-date version of junit 4 (and doesn't address junit 5), so double-check you're installing junit 4. I haven't touched java in a while, so I don't know if there's been major changes with junit 5.

If you downloaded the most recent version of Junit 4 from that GitHub link (4.13.2), then you'll need to modify the CLASSPATH export with that version in your zshrc/bash_profile: export CLASSPATH="$CLASSPATH:$JUNIT_HOME/junit-4.13.2.jar:$JUNIT_HOME/hamcrest-core-1.3.jar"

Also double-check that you downloaded BOTH the junit jar and the hamcrest core jar.

Don't forget to source your zshrc or bash setup.

I hope that helps!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment