Skip to content

Commit

Permalink
Added JavaDoc Comments and created Unit Testing for menu
Browse files Browse the repository at this point in the history
Carter Morrison <40690697@live.napier.ac.uk>

Signed-off-by: Carter Morrison <40690697@live.napier.ac.uk>
  • Loading branch information
WalterWQ committed Nov 26, 2024
1 parent ef21447 commit 5d928c2
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,33 @@
<artifactId>h2</artifactId>
<version>2.2.224</version>
</dependency>
<!-- JUnit 5 dependency -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<!-- Maven Surefire Plugin for running tests -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<includes>
<include>**/UnitTestingMenu.java</include>
</includes>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-jar-plugin</artifactId>
Expand Down
96 changes: 96 additions & 0 deletions src/test/java/group8/project_files/UnitTestingMenu.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 5d928c2

Please sign in to comment.