Unit Testing
As you program larger, more complex, projects it is vitally important that you have confidence the most basic aspects of your code works correctly by automatically testing it. JUnit is the most common automated framework for Java that focuses on testing individual methods. It has been important in the development of test-driven development, TDD, and inspiring similar frameworks in every other modern language.
Getting Started
For this lab exercise, it is best to do the coding individually so everyone gets practice using JUnit, but you are encouraged to discuss your ideas and issues with others around you:
- Fork the original lab_testing repository into your own account to provide a place to edit, commit, and push changes
- Clone the forked repository to your machine so you can work on it locally within IntelliJ
- Import the project into IntelliJ (
New -> Project From Existing Sources)using the folder you just created (and just accept the defaults for all the options by selectingNext)
- Once the project window appears, you will need to do a few more steps to get it to run:
- Select
File -> Project Structureand then selectLibrarieswithin the dialog that appears - Right click on the
junitlibrary and selectAdd to Modules - Click
OKin the dialog that appears andOKagain to close the Project Settings dialog - Click to expand the folders
src -> garageto find the Java classMain - Right click on this class to run it by selecting "Run Main.main()"
- Select
- Run the program to verify that your Java installation is working.
If there are compilation errors or it does not run, then the project is not correctly configured.
Convert Code into Tests
This project is simply an example solution to the previous lab with the proper JUnit libraries included that includes a Main class that simply creates some instances to call methods on and prints out the results. Your first task will be to convert the Main class into several JUnit tests (in fact, after you are done you can delete the class entirely if you want!).
To start, let IntelliJ help you with the boilerplate needed to make a JUnit test by highlighting the class name in the ParkingGarage file and pressing option-return to get a list of available actions:
- Choose "Create Test"
- Just click OK to dismiss the first warning dialog that appears about not having a Test Root
- Within the dialog box that appears, choose JUnit5 instead of the default
- Then select one or more methods to "Generate Tests For" at the bottom of the dialog box
- Then click OK to create a new class named
ParkingGarageTest
This is a regular Java class, but it should now have one or more methods annotated with @Test — these are the tests that will be run.
Move the code from the Main class into the appropriate testing methods (and give them more meaningful names!) and change the println() statements into the appropriate kind of JUnit assert statement.
As you write your tests, consider what common setup code can be factored out into a separate method that is run before each test. This code should go into a method that is typically called setup() but, more importantly, is annotated with @BeforeEach.
Make Tests of Your Own
Once you have converted the existing code to JUnit Tests, create at least 5 new tests for code that is not covered by the existing tests (such as sorting multiple garages).
Write each test in a separate well named method that shows the intention of the test, each annotated with @Test and each with their own call to the appropriate kind of JUnit assert statement. Try to come up with values that you can verify yourself rather than running your code and copying its output as the “expected” value. Remember to test both good and bad values to make sure your code is reasonably robust.
If you are having trouble coming up with tests, you can use IntelliJ to show you what code has not been covered by one of your tests by selecting the "Run with Coverage" option from the Run menu. This will give you a "coverage" report and highlight the to show which lines have been tested and which have not.
When you think you are done, compare your tests to others to see what they thought of that you did not.
Testing your Project
If you have extra time, pair up with someone else and pick a class from each person's Game project to discuss what tests make sense to write for it and how you might write them as JUnit Tests. If you do not have any classes for which you feel would be easy to write Unit Tests, then spend some time discussing how to refactor the code so that it can be tested more easily.
Resources
Submission
At the end of class, use Gitlab's Merge Request to submit your code to the original organization repository (note, there is no discussion file for this lab). Make sure your NetID is in the title of your Merge Request and in comments within the main class.