CSCI-1320 Program Design and Development I

Instructor: Todd Dole

Semester: Fall 2024

Installing JUnit with JGrasp

Introduction

JUnit is a popular testing framework for Java. It allows coders to create unit tests, to test individual units of source code to ensure that each piece of a program is working as intended.

JGrasp supports JUnit 4. This is a slightly older version of the JUnit platform, but is more than adequate for us to learn the basics of writing unit tests in P1.

This guide will cover installing JUnit and configuring JGrasp to use it.

The JUnit Files

You will need to download two .jar files for JUnit. .jar stands for Java Archive. JAR files contain java classes and other resources, packaged into an easy to distribute format.

Here are direct links. Download these files, and place them into a new folder where you will be able to find them later. (For Windows, I recommend you create a JUnit-4 folder in Program Files (x86).

  • junit-4.13.2.jar
  • hamcrest-3.0.jar
  • Configuring JGrasp

    Once you have downloaded the files, in JGrasp, go to the Tools Menu and select JUnit-->Configure

    Click the Browse button next to JUnit Home, and select the folder where you saved the .jar files earlier. Then click ok.

    Creating a Project

    In order to use JUnit, we need to create a Project in JGrasp.

    In JGrasp, Select the Project menu and click on New Project. Pick a name for your project--you can call it P1 or something similar. Click the box for Create New Directory, and then click next. Click next again, and then click Create.

    Setting Classpath

    Next, we need to tell our project where the .jar files are located.

    Go to the Settings Menu, select PATH / CLASSPATH, and choose your project from the menu (Project P1, or whatever you called it in the earlier step.)

    Select the CLASSPATHS tab, and click new.

    Click the top Browse button, next to "Directory Path or JAR File". Navigate to where you saved the .jar files earlier, and select the first file, then click ok.

    Repeat this process for the second .jar file. When you are done, you should have two entries under CLASSPATH.

    Finally, click Apply and Ok to save.

    New Buttons

    If you were successful, you should now have several new buttons:

  • An "Edit JUnit Test File" button, which lets you create a test file for your current class.
  • A "Compile and run Junit Tests" button, which will run the tests in your test file.
  • Steps to Creating JUnit Tests

  • First, create a new .java file with the class you want to create (Just like before, but now it will go into the project src folder)
  • Once you have created your class and at least one method, click the "Edit Junit Test File" button. This will open a new class file for unit tests.
  • Delete the default @Test section it created automatically, and replace it with a @Test section to test your method.
  • Once your test is written, you can click the "Compile and run Junit Tests" buttons to run the tests.