diff --git a/Dockerfile b/Dockerfile index 0b000e7..b60a5f1 100644 --- a/Dockerfile +++ b/Dockerfile @@ -14,7 +14,7 @@ WORKDIR /app COPY . /app # Run Maven to install dependencies and build the project (produces the .jar) -RUN mvn clean package -DskipTests +RUN mvn clean test package # Expose the port your application will run on (default for Spring Boot is 8080) EXPOSE 8080 diff --git a/pom.xml b/pom.xml index 604ce14..4b7b98f 100644 --- a/pom.xml +++ b/pom.xml @@ -26,10 +26,33 @@ h2 2.2.224 + + + org.junit.jupiter + junit-jupiter-api + 5.8.2 + + + + org.junit.jupiter + junit-jupiter-engine + 5.8.2 + + + + org.apache.maven.plugins + maven-surefire-plugin + 3.0.0-M5 + + + **/UnitTestingMenu.java + + + org.apache.maven.plugins maven-jar-plugin diff --git a/src/test/java/group8/project_files/UnitTestingMenu.java b/src/test/java/group8/project_files/UnitTestingMenu.java new file mode 100644 index 0000000..bd3075d --- /dev/null +++ b/src/test/java/group8/project_files/UnitTestingMenu.java @@ -0,0 +1,96 @@ +package group8.project_files; + +import org.junit.jupiter.api.*; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.io.PrintStream; +import java.util.Scanner; + +import static org.junit.jupiter.api.Assertions.assertTrue; + +/** + * Class that contains all the unit testing for the full program + */ +public class UnitTestingMenu { + + /** + * Initialise Variables + */ + private Menu menu; + private ByteArrayOutputStream outputStream; + + /** + * Setup Testing and initialise menu for testing + */ + @BeforeEach + public void setup() { + menu = new Menu(); + outputStream = new ByteArrayOutputStream(); + System.setOut(new PrintStream(outputStream)); + } + + /** + * Checks and sees if the menu works with the exit function and closes correctly + */ + @Test + public void testExit() { + // Simulating the input "exit" + simulateInput("exit\n"); + + // Running the menu start + menu.start(new Scanner(System.in)); + + // Fetching the output produced by the menu + String output = outputStream.toString(); + + // Asserting if the program prints "Exiting the program" + assertTrue(output.contains("Exiting the program"), "The program should print 'Exiting the program' when 'exit' is entered."); + } + + /** + * Checks and verify if the invalid input detections works and ensures that users can only select correct menu options + */ + @Test + public void testInvalidOption() { + // Simulating an invalid option followed by "exit" + simulateInput("invalid\nexit\n"); + + // Running the menu start + menu.start(new Scanner(System.in)); + + // Fetching the output produced by the menu + String output = outputStream.toString(); + + // Asserting if the program responds to invalid input + assertTrue(output.contains("Invalid option"), "The program should print 'Invalid option' when invalid input is entered."); + } + + /** + * Ensures the menu options are able to be selected and ran + */ + @Test + public void testOptionSelection() { + // Simulating valid input "1" followed by "exit" + simulateInput("1\nexit\n"); + + // Running the menu start + menu.start(new Scanner(System.in)); + + // Fetching the output produced by the menu + String output = outputStream.toString(); + + // Asserting if the program responds to valid option + assertTrue(output.contains("Option 1 selected!"), "The program should print 'Option 1 selected!' when '1' is entered."); + } + + + /** + * Simple function which simulates user input, Pass a string/data to this and it will be pushed to the menu like a user would do + * @param data + */ + private void simulateInput(String data) { + InputStream inputStream = new ByteArrayInputStream(data.getBytes()); + System.setIn(inputStream); + } +}