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.
Learning Outcomes
- Create a
test/
source folder - Create a package in the
test/
folder - Create a class for testing
- Run JUnit tests
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!
Reference: Creating Packages and Classes in Eclipse
References
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. Entertest
in 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
test
source folderedu.ncsu.csc216.wolf_scheduler.course
package namedCourseTest
. Notice you use the same class name (Course
) and append the wordTest
to the end of the class name.
- Copy the code from
CourseTest.java
into yourCourseTest
class. The class will not compile! That is because you are missing a library.
- You need to add the JUnit library to your project. Right click on the
WolfScheduler
project and select Properties. In the left menu, select Java Build Path. In the right portion of the screen, select the Libraries tab. Select Classpath and then click Add Library…. Select JUnit and click Next. Select JUnit 5 in the drop down and click Finish followed by Apply and Close. When you are done, the JUnit 5 library will be added to the project’s build path and the compiler errors should be resolved.
- With JUnit 5, we can parameterize our tests with information stored in comma-separated value (CSV) files. Right-click on the
test
folder and select New > Folder. Enterresources
in the Folder name text field. Click Finish. This will create a package icon that appears empty, but it is a folder!
-
Download and move the following files into your
test/resources/
folder. You can also create new files with a.csv
extension and copy in the text.
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!
Reference: Staging and Pushing to GitHub
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
CourseTest
,course-meeting-days-and-times-valid.csv
,course-meeting-days-and-times-invalid.csv
to the index. - Commit and push the
CourseTest
class, the CVS files, and anyCourse
changes to GitHub. Remember to use a meaningful commit message describing how you have changed the code. For example, “[Test] Added CourseTest and related files”.