Question: In Java 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)
In Java
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 create a 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.
public class HardwareStore { private ArrayList- itemList; private static final String DATA_FILE_NAME = "database.txt"; /** * This constructor creates an empty ArrayList and then calls the *
readDatabase() method to populate items previously stored. * * @throws IOException */ public HardwareStore() throws IOException { itemList = new ArrayList<>(); readDatabase(); } /** * Method getAllItemsFormatted returns the current list of items in the Arraylist in * no particular order. * * @return a formatted String representation of all the items in itemList. */ public String getAllItemsFormatted() { return getFormattedItemList(itemList); } /** * Private method getFormattedPackageList used as an auxiliary method to return a given ArrayList * of items in a formatted manner. * * @param items the item list to be displayed. * @return a formatted String representation of all the items in the list give as a parameter. */ private String getFormattedItemList(ArrayList- items) { String text = " ------------------------------------------------------------------------------------ " + String.format("| %-10s| %-25s| %-20s| %-10s| %-10s|%n", "ID Number", "Name", "Category", "Quantity", "Price") + " ------------------------------------------------------------------------------------ "; for (int i = 0; i < items.size(); i++) { text += String.format("| %-10s| %-25s| %-20s| %-10s| %-10s|%n", items.get(i).getIdNumber(), items.get(i).getName(), items.get(i).getCategory(), Integer.toString(items.get(i).getQuantity()), String.format("%.2f", items.get(i).getPrice())); } text += " ------------------------------------------------------------------------------------ "; return text; } /** * This method is used to add a item to the itemList ArrayList. * * @param idNumber a
String representing the ID number of item * @param name a String representing the name of item * @param category a String representing the category of item * @param quantity an int representing the quantiy of item * @param price a float representing the price of item */ public void addNewItem(String idNumber, String name, String category, int quantity, float price) { //If passed all the checks, add the item to the list itemList.add(new Item(idNumber, name, category, quantity, price)); System.out.println("Item has been added. "); } /** * Add a certain quantity of the given item index. * Preconditions: 1. Item exists. * @param itemIndex the index of the item in the itemList * @param quantity the quantity to remove */ public void addQuantity(int itemIndex, int quantity) { Item temp = getItem(itemIndex); temp.setQuantity(temp.getQuantity() + quantity); System.out.println("Quantity updated. "); } /** * Removes a certain quantity of the given item index. * Preconditions: 1. Item exists. 2. Quantity to remove smaller than current quantity. * @param itemIndex the index of the item in the itemList * @param quantity the quantity to remove */ public void removeQuantity(int itemIndex, int quantity) { Item temp = getItem(itemIndex); temp.setQuantity(temp.getQuantity() - quantity); System.out.println("Quantity updated. "); } /** * Returns all the items that (partially) match the given name. * @param name the name to match. * @return a string containing a table of the matching items. */ public String getMatchingItemsByName(String name) { ArrayList- temp = new ArrayList
- (); for (Item tempItem : itemList) { if (tempItem.getName().toLowerCase().contains(name.toLowerCase())) { temp.add(tempItem); } } if (temp.size() == 0) { return null; } else { return getFormattedItemList(temp); } } /** * Returns all the items with current quantity lower than (or equal) the * given threshold. * @param quantity the quantity threshold. * @return a string containing a table of the matching items. */ public String getMatchingItemsByQuantity(int quantity) { ArrayList
- temp = new ArrayList
- (); for (Item tempItem : itemList) { if (tempItem.getQuantity() <= quantity) { temp.add(tempItem); } } if (temp.isEmpty()) { return null; } else { return getFormattedItemList(temp); } } /** * This method can be used to find a item in the Arraylist of items. * * @param idNumber a
String that represents the ID number of * the item that to be searched for. * @return the int index of the items in the Arraylist of * items, or -1 if the search failed. */ public int findItem(String idNumber) { int index = -1; for (int i = 0; i < itemList.size(); i++) { String temp = itemList.get(i).getIdNumber(); if (idNumber.equalsIgnoreCase(temp)) { index = i; break; } } return index; } /** * This method is used to retrieve the Item object from the * itemList at a given index. * * @param i the index of the desired Item object. * @return the Item object at the index or null if the index is * invalid. */ public Item getItem(int i) { if (i < itemList.size() && i >= 0) { return itemList.get(i); } else { System.out.println("Invalid Index. "); return null; } } /** * This method opens the database file and overwrites it with a * text representation of all the items in the itemList. This * should be the last method to be called before exiting the program. * * @throws IOException */ public void writeDatabase() throws IOException { PrintWriter pw = new PrintWriter(DATA_FILE_NAME); for (Item c : itemList) { pw.print(c.toString()); } pw.close(); } /** * The method opens the database file and initializes the itemList * with its contents. If no such file exists, then one is created. * The contents of the file are "loaded" into the itemList ArrayList in no * particular order. The file is then closed during the duration of the * program until writeDatabase() is called. * * @throws IOException */ public void readDatabase() throws IOException { File dataFile = new File(DATA_FILE_NAME); // If data file does not exist, create it. if (!dataFile.exists()) { System.out.println("database.txt does not exist, creating one now . . ."); //if the file doesn't exists, create it PrintWriter pw = new PrintWriter(DATA_FILE_NAME); //close newly created file so we can reopen it pw.close(); return; // No need to try to read anything from an empty file, so return. } Scanner itemScanner = new Scanner(new FileReader(dataFile)); //Initialize the Array List with items from database.txt while (itemScanner.hasNextLine()) { // split values using the space character as separator String[] temp = itemScanner.nextLine().split("~"); itemList.add(new Item(temp[0], temp[1], temp[2], Integer.parseInt(temp[3]), Float.parseFloat(temp[4]))); } //item list is now in the ArrayList completely so we can close the file itemScanner.close(); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
