To ensure high quality software, it must be tested. There are many forms of testing, at different levels in the software.
This lesson teaches unit testing, in which each component is tested in isolation from
other components that it depends upon. In object-oriented development, the most common component is the class/object.
Learning Outcomes
Identify and plan effective test cases
Create unit tests with an assertions library
Create mock objects for unit tests
Study Resources
For your study of this topic, use these resources.
This course has many modules. Feel free to watch as many as you want to fill-in your knowledge of using JUnit for unit testing Java applications. You are required to watch the following modules:
These videos were done before the release of JUnit 5 which is what you will be using. JUnit 5 has added many features that
you will probably not use. There are some notable differences with what you will see in these videos and JUnit 5 unit tests. These
include:
All the assertion statements can take a message but it is the last parameter in the assertion call rather than the first.
You use an assertThrows assertion for testing that an exception is thrown rather than specifying this on the
@Test line before the test method begins. assertThrows is given the exception class that is expected to be
thrown, and an executable that should throw the exception.
Refer to the unit tests written for the sample webapp which all meet the JUnit 5 requirements.
Click here for the slides and instructions to your individual activity. The code examples are from the
Heroes REST API project that you used for the REST API Basics Spike. The project also contains a full suite of unit tests, which should all pass if you corrently implemented all the APIs for this Spike.
Each team member will create unit tests for 2 classes in your term project and commit them to the GitHub repo. As a team, decide on which classes in the Controller, Model or Persistence tier can be divided up. If there are not enough classes in your project for each member to work on 2 separate classes, teams can pick one class for themselves and another they will work with another team member. For the shared class, coordinate the tests to ensure there are no duplicate test cases.
Each team member will submit the two Java source files containing the unit tests to the Unit testing - individual Assignments folder in the Exercises area of myCourses.
Project Activities
The Definition of Done checklist in the STORY template card in your Trello planning board includes checklist items for "unit tests written" and "solution is code complete, i.e. passes full suite of unit tests". From this point, when you implement a story you will include unit testing as
part of your sprint process.
End-Of-Day Exercises
Unit testing - quiz (2 exercise points)
Watch all of the videos listed above, which provides more details on Unit Testing and JUnit.
After watching the videos, by the time indicated on the schedule for your section, complete the 5-minute quiz, Unit testing,
which is in the myCourses Quiz area.