A unit test is an automated piece of code that invokes a unit of work in the system and then checks a single assumption about the behavior of that unit of work. A unit of work is a single logical functional use case in the system that can be invoked by some public interface (in most cases). A unit of work can span a single method, a whole class or multiple classes working together to achieve one single logical purpose that can be verified.

Think of unit testing as a way to test the behavior of the code (or parts of the code) written, without actually having to run the program. For example, in the case Assignment 1, assume that the front-end (MainApp console user interface) part of the program and the back-end part of the program (Hardware Store database management) are written by two different developers. How would the developer of the back-end be able ensure that the code he/she has written works correctly without having access to the front-end?

In this assignment, you are asked to create JUnit tests in the class HardwareStoreTest to test the class Hardware Store. You are tasked with creating Junit tests for all HardwareStore.java methods except the methods us for reading from and writing to the data file, as written for Assignment1. No need to test the classes MainApp and Item.

Task 1: Create Junit tests for the behavior of the class/methods under normal operation scenarios. For example, to test the method findItem(String id) of the class HardwareStore, you may need to creata test method, which creates a mock Item object and adds it to the itemList before the method findItem can be called to search the list. To ensure that everything worked as planned, you can then search for the item using its idNumber and see if the correct item is found. Mock objects can be created either in the same test method or before any test methods are run, using the @BeforeClass or @Before annotation.

Task 2: Create test cases for unusual console inputs, e.g. when it is expected to cause an error, or not as expected. Testing the methods that produce output to the console can be a little tricky. The whole output needs to be captured into a String (by redirecting System.out to a PrintStream) and then that String needs to be compared to the expected output.

Task 3: Create a TestRunner class, which has a main method that runs the test class HardwareStoreTest that you have created and prints out the test results in command line.

Note: Do not modify the code of the solution to Assignment 1 provided to make your testing easier.

Tasks:

1. Implement the JUnit tests to test only the class HardwareStore.java. Try to be creative by coming up with test cases that can test as many different situations as possible. Do not test the classes Item.java and MainApp.java.

2. Use a standard Java coding style to improve your program's visual appearance and make it more readable. I suggest the BlueJ coding style: http://www.bluej.org/objects-first/styleguide.html

3. Use Javadoc to document your code.

Academic Honesty!
It is not our intention to break the school's academic policy. Posted solutions are meant to be used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly complete the form. Please provide a valid email address and we will get back to you within 24 hours. Payment is through PayPal, Buy me a Coffee or Cryptocurrency. We are a nonprofit organization however we need funds to keep this organization operating and to be able to complete our research and development projects.