russkiymalchik / code-refactoring-22-23

0 stars 0 forks source link

Unit Testing_Hotel Management_Lab2 #17

Open russkiymalchik opened 1 year ago

russkiymalchik commented 1 year ago

I would like to begin by presenting my 2nd laboratory report, in which I have developed comprehensive set of 17 unit tests of a Java application. These tests can be accessed on the corresponding repository, which I will provide for your reference.
Source : Hotel Management Testing 1

testMainMethodWithUserInput_LuxuryDoubleRoom()
This part of the unit testing is checking on the shotcoming that appear on the main class which is "Conditional Complexity" by trying an unit test on a specific case of "Luxury Double Room" ``` @Test public void testMainMethodWithUserInput_LuxuryDoubleRoom() { // Prepare the input String input = "1\n1\nn\n"; ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes()); System.setIn(inputStream); // Call the method being tested HotelMain.main(new String[]{}); // Verify the output // Verify the output String expectedOutput = "Welcome to Paradise Inn\n\nEnter your choice :\n1.Display room details\n2.Display room availability \n3.Book\n4.Order food\n5.Checkout\n6.Exit\n\n" + "\nChoose room type :\n1.Luxury Double Room \n2.Deluxe Double Room \n3.Luxury Single Room \n4.Deluxe Single Room\n" + "\nNumber of double beds : 1\nAC : Yes\nFree breakfast : Yes\nCharge per day:4000 \n"+ "\nContinue : (y/n)"; assertEquals(expectedOutput, outputStream.toString().trim()); } ``` This unit testing provide us testing with an input based on ``` String input = "1\n1\nn\n"``` that direct us to the "Luxury Double Room"
testMainMethodWithUserInput_DeluxeDoubleRoom()
This part of the unit testing is checking on the shotcoming that appear on the main class which is "Conditional Complexity" by trying an unit test on a specific case of "Deluxe Double Room" ``` @Test public void testMainMethodWithUserInput_DeluxeDoubleRoom() { // Prepare the input String input = "1\n2\nn\n"; ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes()); System.setIn(inputStream); // Call the method being tested HotelMain.main(new String[]{}); // Verify the output // Verify the output String expectedOutput = "Welcome to Paradise Inn\n\nEnter your choice :\n1.Display room details\n2.Display room availability \n3.Book\n4.Order food\n5.Checkout\n6.Exit\n\n" + "\nChoose room type :\n1.Luxury Double Room \n2.Deluxe Double Room \n3.Luxury Single Room \n4.Deluxe Single Room\n" + "\nNumber of double beds : 1\nAC : No\nFree breakfast : Yes\nCharge per day:3000 \n"+ "\nContinue : (y/n)"; assertEquals(expectedOutput, outputStream.toString().trim()); } ``` This unit testing provide us testing with an input based on ``` String input = "1\n2\nn\n"``` that direct us to the "Deluxe Double Room"
testMainMethodWithUserInput_LuxurySingleRoom()
This part of the unit testing is checking on the shotcoming that appear on the main class which is "Conditional Complexity" by trying an unit test on a specific case of "Luxury Single Room" ``` @Test public void testMainMethodWithUserInput_LuxurySingleRoom() { // Prepare the input String input = "1\n3\nn\n"; ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes()); System.setIn(inputStream); // Call the method being tested HotelMain.main(new String[]{}); // Verify the output // Verify the output String expectedOutput = "Welcome to Paradise Inn\n\nEnter your choice :\n1.Display room details\n2.Display room availability \n3.Book\n4.Order food\n5.Checkout\n6.Exit\n\n" + "\nChoose room type :\n1.Luxury Double Room \n2.Deluxe Double Room \n3.Luxury Single Room \n4.Deluxe Single Room\n" + "\nNumber of single beds : 1\nAC : Yes\nFree breakfast : Yes\nCharge per day:2200 \n"+ "\nContinue : (y/n)"; assertEquals(expectedOutput, outputStream.toString().trim()); } ``` This unit testing provide us testing with an input based on ``` String input = "1\n3\nn\n"``` that direct us to the "Luxury Single Room"
testMainMethodWithUserInput_DeluxeSingleRoom()
This part of the unit testing is checking on the shotcoming that appear on the main class which is "Conditional Complexity" by trying an unit test on a specific case of "Luxury Single Room" ``` @Test public void testMainMethodWithUserInput_DeluxeSingleRoom() { // Prepare the input String input = "1\n4\nn\n"; ByteArrayInputStream inputStream = new ByteArrayInputStream(input.getBytes()); System.setIn(inputStream); // Call the method being tested HotelMain.main(new String[]{}); // Verify the output // Verify the output String expectedOutput = "Welcome to Paradise Inn\n\nEnter your choice :\n1.Display room details\n2.Display room availability \n3.Book\n4.Order food\n5.Checkout\n6.Exit\n\n" + "\nChoose room type :\n1.Luxury Double Room \n2.Deluxe Double Room \n3.Luxury Single Room \n4.Deluxe Single Room\n" + "\nNumber of single beds : 1\nAC : Yes\nFree breakfast : Yes\nCharge per day:2200 \n"+ "\nContinue : (y/n)"; assertEquals(expectedOutput, outputStream.toString().trim()); } ``` This unit testing provide us testing with an input based on ``` String input = "1\n3\nn\n"``` that direct us to the "Luxury Single Room"
testConditionalComplexity()
This part of the unit testing is checking on the shotcoming that appear on the main class which is "Conditional Complexity" by trying an unit test on a specific case of book a room of 61" ``` @Test public void testConditionalComplexity() { // Prepare the input String userInput = "4\n61\nn"; InputStream inputStream = new ByteArrayInputStream(userInput.getBytes()); System.setIn(inputStream); // Capture the output ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); System.setOut(new PrintStream(outputStream)); // Call the method being tested HotelMain.main(new String[]{}); // Verify the output String expectedOutput = "Welcome to Paradise Inn\n\nEnter your choice :\n1.Display room details\n2.Display room availability \n3.Book\n4.Order food\n5.Checkout\n6.Exit" + "\n\nRoom Number -"+ "Room doesn't exist\n"+ "\nContinue : (y/n)"; assertEquals(expectedOutput, outputStream.toString().trim()); } ``` This unit testing provide us testing with an input based on ```String userInput = "4\n61\nn";``` that direct us to the "Room doesn't exist"
testFoodCreation()
This part of the unit testing is checking on the variables inside the ```Food``` class and also to showcase the potential primitive obsession in the Food class by focusing on the direct manipulation of primitive types (itemno and quantity) ``` @Test public void testFoodCreation() { // Create a Food object with item number 1 and quantity 5 Food food = new Food(1, 5); // Verify the item number and quantity assertEquals(1, food.itemno); assertEquals(5, food.quantity); } ``` This unit testing provide us testing with an input based on ```Food food = new Food(1, 5);```
testFoodCreation2()
This part of the unit testing is checking on the variables inside the ```Food``` class and also to showcase the potential primitive obsession in the Food class by focusing on the direct manipulation of primitive types (itemno and quantity) ``` @Test public void testFoodCreation2() { // Create a Food object with item number 1 and quantity 5 Food food = new Food(2, 3); // Verify the item number and quantity assertEquals(2, food.itemno); assertEquals(3, food.quantity); } ``` This unit testing provide us testing with an input based on ```Food food = new Food(2, 3);```
testFoodPriceCalculation()
This part of the unit testing is checking the calculations that runs inside the ```Food``` class and also to showcase the potential primitive obsession in the Food class by focusing on the direct manipulation of primitive types (itemno and quantity) ``` @Test public void testFoodPriceCalculation() { int itemNo = 1; int quantity = 3; Food food = new Food(itemNo, quantity); float expectedPrice = 150.0f; assertEquals(expectedPrice, food.getPrice()); } ``` This unit testing provide us testing with an input based on ``` int itemNo = 1; int quantity = 3; ```
testFoodPriceCalculation2()
This part of the unit testing is checking the calculations that runs inside the ```Food``` class and also to showcase the potential primitive obsession in the Food class by focusing on the direct manipulation of primitive types (itemno and quantity) ``` @Test public void testFoodPriceCalculation2() { int itemNo = 2; int quantity = 7; Food food = new Food(itemNo, quantity); float expectedPrice = 420.0f; assertEquals(expectedPrice, food.getPrice()); } ``` This unit testing provide us testing with an input based on ``` int itemNo = 2; int quantity = 7; ```
testFoodPriceCalculation2()
This part of the unit testing is trying to perform serialization and deserialization inside the ```Food``` class and also to showcase the potential primitive obsession in the Food class by focusing on the direct manipulation of primitive types (itemno and quantity) ``` @Test public void testSerialization() throws Exception { int itemNo = 2; int quantity = 2; Food food = new Food(itemNo, quantity); // Serialize the Food object ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream); objectOutputStream.writeObject(food); objectOutputStream.flush(); objectOutputStream.close(); // Deserialize the Food object (just to show that serialization/deserialization works) // You can add more assertions to test the deserialized object if needed Food deserializedFood = (Food) new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray())).readObject(); assertEquals(food.getItemNo(), deserializedFood.getItemNo()); assertEquals(food.getQuantity(), deserializedFood.getQuantity()); assertEquals(food.getPrice(), deserializedFood.getPrice()); } ``` This unit testing provide us testing with an input based on ``` int itemNo = 2; int quantity = 2; ``` This method tests the serialization and deserialization functionality of the Food class. It creates a Food object, serializes it to a byte array, and then deserializes it back to a new Food object. It verifies that the deserialized object retains the same item number, quantity, and price as the original object. These tests help demonstrate the potential primitive obsession in the Food class by focusing on the direct manipulation of primitive types
testDataClumps_LuxuryDoubleRooms()
This part of the unit testing is trying to perform a test on a potential data clumps issue on ```holder``` class ``` @Test public void testDataClumps_LuxuryDoubleRooms() { holder holder = new holder(); Doubleroom[] luxuryDoubleRooms = holder.getLuxuryDoubleRooms(); // Verify the relationships between the data clumps assertNotNull(luxuryDoubleRooms); // Perform assertions on the data clumps as needed assertEquals(10, luxuryDoubleRooms.length); } ``` In this test, we create an instance of the holder class and access its data clumps (arrays of rooms). We verify that the data clumps are not null and check their expected lengths. This demonstrates the presence of closely related data fields that are grouped together, which is indicative of the "data clumps" issue.
testDataClumps_DeluxeDoubleRooms()
This part of the unit testing is trying to perform a test on a potential data clumps issue on ```holder``` class ``` @Test public void testDataClumps_DeluxeDoubleRooms() { holder holder = new holder(); Doubleroom[] deluxeDoubleRooms = holder.getDeluxeDoubleRooms(); // Verify the relationships between the data clumps assertNotNull(deluxeDoubleRooms); // Perform assertions on the data clumps as needed assertEquals(20, deluxeDoubleRooms.length); } ``` In this test, we create an instance of the holder class and access its data clumps (arrays of rooms). We verify that the data clumps are not null and check their expected lengths. This demonstrates the presence of closely related data fields that are grouped together, which is indicative of the "data clumps" issue.
testDataClumps_LuxurySingleRooms()
This part of the unit testing is trying to perform a test on a potential data clumps issue on ```holder``` class ``` @Test public void testDataClumps_LuxurySingleRooms() { holder holder = new holder(); Singleroom[] luxurySingleRooms = holder.getLuxurySingleRooms(); // Verify the relationships between the data clumps assertNotNull(luxurySingleRooms); // Perform assertions on the data clumps as needed assertEquals(10, luxurySingleRooms.length); } ``` In this test, we create an instance of the holder class and access its data clumps (arrays of rooms). We verify that the data clumps are not null and check their expected lengths. This demonstrates the presence of closely related data fields that are grouped together, which is indicative of the "data clumps" issue.
testDataClumps_DeluxeSingleRooms()
This part of the unit testing is trying to perform a test on a potential data clumps issue on ```holder``` class ``` @Test public void testDataClumps_DeluxeSingleRooms() { holder holder = new holder(); Singleroom[] deluxeSingleRooms = holder.getDeluxeSingleRooms(); // Verify the relationships between the data clumps assertNotNull(deluxeSingleRooms); // Perform assertions on the data clumps as needed assertEquals(20, deluxeSingleRooms.length); } ``` In this test, we create an instance of the holder class and access its data clumps (arrays of rooms). We verify that the data clumps are not null and check their expected lengths. This demonstrates the presence of closely related data fields that are grouped together, which is indicative of the "data clumps" issue.
testIndecentExposure_Doubleroom_name2()
This part of the unit testing is trying to debunk the potential of indecent exposure in ```Doubleroom``` class by examining the ```name2``` ``` @Test public void testIndecentExposure_Doubleroom_name2() throws NoSuchFieldException { Field name2Field = Doubleroom.class.getDeclaredField("name2"); assertFalse(name2Field.isAccessible()); } ``` In this test, we use reflection to access the fields name2 in the Doubleroom class. By asserting that these fields are not accessible (isAccessible() returns false), we highlight the issue of "Indecent Exposure" where fields can be accessed from outside the class.
testIndecentExposure_Doubleroom_contact2()
This part of the unit testing is trying to debunk the potential of indecent exposure in ```Doubleroom``` class by examining the ```contact2``` ``` @Test public void testIndecentExposure_Doubleroom_contact2() throws NoSuchFieldException { Field contact2Field = Doubleroom.class.getDeclaredField("contact2"); assertFalse(contact2Field.isAccessible()); } ``` In this test, we use reflection to access the fields ```contact2``` in the ```Doubleroom``` class. By asserting that these fields are not accessible (isAccessible() returns false), we highlight the issue of "Indecent Exposure" where fields can be accessed from outside the class.
testIndecentExposure_Doubleroom_gender2()
This part of the unit testing is trying to debunk the potential of indecent exposure in ```Doubleroom``` class by examining the ```gender2``` ``` @Test public void testIndecentExposure_Doubleroom_gender2() throws NoSuchFieldException { Field gender2Field = Doubleroom.class.getDeclaredField("gender2"); assertFalse(gender2Field.isAccessible()); } ``` In this test, we use reflection to access the fields ```gender2``` in the ```Doubleroom``` class. By asserting that these fields are not accessible (isAccessible() returns false), we highlight the issue of "Indecent Exposure" where fields can be accessed from outside the class.

Result

image

In the results section of my 2nd laboratory report, I am pleased to report that the testing work conducted has been carried out effectively and with proper execution. However, it is important to note that there were 5 tests that did not meet the expected values. Despite extensive analysis and troubleshooting over several days, the test results remained consistent, with the expected values matching the actual values exactly, as illustrated below.

image ref : testConditionalComplexity()



In summary, I successfully implemented a total of 17 sets of unit tests as part of my analysis and evaluation of the code. The primary objective was to identify and address any potential shortcomings present within the application 😊 ✌️ 💖 .