Skip to main content

Guided Task: A Very Brief Introduction to Unit Testing

Larger systems with many interacting classes only have one main method. If you can’t start a program from a main method, you are unable to run black box tests on the program.

You could wait until all of the classes are implemented to start testing, but that is very hard debug - and there are always bugs! Instead, you should test as you go along to ensure that your program’s smaller units (classes and methods) work the way they are supposed to. For the WolfScheduler project, the teaching staff are providing a suite of JUnit tests that you can run to ensure that your implementation meets the specified WolfScheduler requirements and design.

Callout Box Icon

Learning Outcomes

  • Create a test/ source folder
  • Create a package in the test/ folder
  • Create a class for testing
  • Run JUnit tests
Callout Box Icon Callout Box Icon

Best Practice: Unit Testing

During the test phase of development, you execute the program against a variety of inputs to evaluate if the program generates the correct or expected output. You can determine the expected output by reading the requirements as well as the design documentation and APIs that define what the smallest program units (methods) should do.

Unit testing focuses on testing the smallest units of a program: methods and classes. The premise is that by ensuring the smallest portions of the programs work correctly, you can have increased confidence that the whole program will work correctly.

When you unit test for CSC 216/217, you are actually doing a combination of unit testing and integration testing. Integration testing is when you test things in combination. You will test methods in combination (e.g., adding something to a list and then removing it) and you will test methods that call other methods (e.g., a constructor calls a setter method). However, your focus is on the very local functionality of that method or combinations of methods within a class and not the program as a whole.

Callout Box Icon

Tool: JUnit

JUnit is an automated unit testing tool for Java. JUnit is a set of libraries that include classes, like TestCase, and methods, like assertEquals(), that help with unit testing Java code.

For Guided Project 1, all unit tests will be provided so you will be able to assess the quality of your work as you progress through the tasks. Later, you will be expected to write your own tests and your implementation will be exercised against a hidden suite of teaching staff tests that will assess how well you meet the teaching staff design on the project!

Setup WolfScheduler for Testing

You’ll start by setting up the WolfScheduler project for testing so you can test Course.

  1. Create a new source folder, called test, which will contain all JUnit test files. Right click on the project and select New > Source Folder. Enter test in the Folder name text field.


Figure: New Source Folder for Test Files
Figure: New Source Folder for Test Files


  1. Create a new package in the test source folder named edu.ncsu.csc216.wolf_scheduler.course. Notice that the package is the same as Course’s package.
  1. Create a new class in the test source folder edu.ncsu.csc216.wolf_scheduler.course package named CourseTest. To do this, select the package in the test source folder and select New > JUnit Test Case. Notice you use the same class name (Course) and append the word Test to the end of the class name. Select the Course name as the Class under test. Click Finish to create the class. You’ll be prompted to add JUnit5 library to the build path. Click OK to do so.


Figure: Create `CourseTest` Class
Figure: Create `CourseTest` Class



Figure: Add JUnit 5 to Build Path
Figure: Add JUnit 5 to Build Path


  1. Copy the code from CourseTest.java into your CourseTest class. The class will not compile! That is because you are missing a library.
  1. Verify JUnit 5 Configuration: After creating CourseTest, you must ensure JUnit 5 is properly added to your project’s build path. Follow these steps:

    a. Check if JUnit 5 appears in the Package Explorer under your project:

    • Expand WolfScheduler
    • Look for JUnit 5 under the project (not under a folder)

    b. If JUnit 5 is missing or you see compilation errors about missing JUnit classes:

    • Right click on the WolfScheduler project
    • Select Properties > Java Build Path
    • Click the Libraries tab
    • Select Classpath (not Modulepath)
    • Click Add Library…
    • Select JUnit and click Next
    • Choose JUnit 5 from the dropdown
    • Click Finish, then Apply and Close


Figure: Add the JUnit 5 Library
Figure: Add the JUnit 5 Library


1
2
3
 c. If errors persist after adding the library:
    - Right click on the project and select **Refresh** (F5)
    - Clean and rebuild: **Project > Clean...**, select WolfScheduler, click **Clean**
Callout Box Icon

Troubleshooting: JUnit Configuration

Common JUnit Issue: If Eclipse shows “JUnit 5 cannot be resolved” even after adding the library, ensure you selected Classpath (not Modulepath) when adding the library. If the issue continues, try: 1) Restarting Eclipse, 2) Checking that you’re using Java SE-21, 3) Asking for help on the forum with a screenshot of your build path configuration.


Figure: Add the JUnit 5 Library
Figure: Add the JUnit 5 Library


Run JUnit Tests

Run the provided JUnit tests by right clicking on the test source folder and selecting Run As > JUnit Test. By running the command on the test source folder, you will run all the test classes in the folder. You can also run all the tests in a package or a single test class by right clicking on that artifact.


Figure: Run JUnit Tests
Figure: Run JUnit Tests


After running the tests, several will be passing (testCourseStringStringStringIntStringString(), testEqualsObject(), testHashCode(), toString()), but several will be failing! That’s Ok! You’ll fix that soon!


Figure: JUnit Results
Figure: JUnit Results


Reference: Staging and Pushing to GitHub

Check Your Progress

You’ve added your CourseTest class. Before moving on to the next portion of the Guided Project, complete the following tasks:

  • Add CourseTest to the index.
  • Commit and push the CourseTest class and any Course changes to GitHub. Remember to use a meaningful commit message describing how you have changed the code. For example, “[Test] Added CourseTest”.