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.
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.
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.
- Create a new source folder, called
test, which will contain all JUnit test files. Right click on the project and select New > Source Folder. Entertestin the Folder name text field.
- Create a new package in the test source folder named
edu.ncsu.csc216.wolf_scheduler.course. Notice that the package is the same asCourse’s package.
- Create a new class in the
testsource folderedu.ncsu.csc216.wolf_scheduler.coursepackage namedCourseTest. To do this, select the package in thetestsource folder and select New > JUnit Test Case. Notice you use the same class name (Course) and append the wordTestto the end of the class name. Select theCoursename 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.
- Copy the code from
CourseTest.javainto yourCourseTestclass. The class will not compile! That is because you are missing a library.
-
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
WolfSchedulerproject - 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
- Expand
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**
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.
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.
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!
GitHub Resources:
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
CourseTestto the index. - Commit and push the
CourseTestclass and anyCoursechanges to GitHub. Remember to use a meaningful commit message describing how you have changed the code. For example, “[Test] Added CourseTest”.
