This project showcases how to run and use JUnit5 without using an IDE. This was required for demoing my coursework at university and it might benefit other students in the same position. If you can run JUnit 5 tests through your IDE, you should probably just do that. See JUnit5 Docs for set-up advice.
This uses the GNU make which is available for unix environments (MacOS & Linux). I'm not sure if this would work with Cmake or how to install for Windows, please see the Java Makefile link below to read more about setting up for Windows. I have included what I believe to be the correct JFlags variable for windows, comment the normal one by placing a # in front (or removing) and uncomment the WindowsJFlags and feel free to update the name.
You can download the files as a zip and just extract the lib folder and makefile into your project. Alternatively git clone this into your project and delete the un-needed files
git clone [email protected]:nathcakes/run-junit5-locally
.
Or just replicate this repo by using the below link to download the launcher, place it in a lib folder and copy the makefile.
If all your files are in the same root directory (as per the example set-up):
- Update the makefile line "Classes: " to the name of your .java files separated with spaces, including the .java file extension.
- In the terminal type:
make
- To run your tests type:
make runTests
- If you update any files, type
make
again, Make will automatically detect which files need to be recompiled - If you need to remove all your .class files for submission, type:
make clean
NOTE: This relies on the junit-console file being in a local lib folder, if you update the name you must update the makefile and if you submit your work INCLUDE THE LIB FOLDER.
I will do my best to keep the junit-console up to date with the latest release, you can manually update the console by replacing the jar file in "./lib/" with the latest release available here. If you are updating the junit-console make sure you also update the makefile. Please feel free to open an issue in GitHub if the console is updated to notify me.
If you separate your files (e.g. "src", "out", "tests" etc.):
- Update the JFLAGS in the makefile to include the dir of your .java files and add a wildcard (*), separated by : e.g. "./lib/*:./src/*:./tests/*"
- Update the JFLAGS in the makefile to include the -d flag for your directory e.g. -d "./out/dev"
- Update the runTests in the makefile to include the dir of your test .class files in the -cp e.g.
java -jar "./lib/junit-platform-console-standalone-1.9.0.jar" -cp ./out/dev/* --scan-classpath
I'm not sure how to update the Clean command, unfortunately. You might also want to make some other make commands, but at this point you're probably better off using an IDE.
In the makefile, you can add the below flags to the runTests command, there are more listed on JUnit5 docs.
- --reports-dir=DIR (Creates a report and stores it in the DIR provided)
- --fail-if-no-tests (If there are no tests, it will exit with a status code 2)
- --details=MODE (Available modes are none,summary,flat,tree,verbose) default is tree.
See herefor additional flags you can pass to the runTests command.
Thanks to Tia Newhall of Swarthmore College, PA USA for her Java makefile examples, this helped me build the Java makefile.
ExampleClass & ExampleTests are adapted from the JUnit-5 samples provided by the junit-team under the Eclipse Public License - v2.0
This project uses code provided under the Eclipse Public License - v2.0 and this project itself is made available under the same license. As of 16 Aug, 2022. You can read the license here: Eclipse Public License - v2.0
Author - Nate Brown 16/08/2022