Add unit tests for generateNumberToBeGuessed() and isCorrectGuess() methods

- Verified structure and value bounds of generateNumberToBeGuessed()
- Tested correct and incorrect input behavior of isCorrectGuess()
- Set up JUnit 5 with @BeforeEach for Game instantiation
- Improved test readability with @DisplayName annotations
This commit is contained in:
Derek L. Seitz 2025-03-23 12:55:32 -05:00
commit 93008219c2
10 changed files with 307 additions and 0 deletions

38
.gitignore vendored Normal file
View File

@ -0,0 +1,38 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
*.iml
*.ipr
### Eclipse ###
.apt_generated
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
build/
!**/src/main/**/build/
!**/src/test/**/build/
### VS Code ###
.vscode/
### Mac OS ###
.DS_Store

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Editor-based HTTP Client requests
/httpRequests/
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml

7
.idea/encodings.xml Normal file
View File

@ -0,0 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="file://$PROJECT_DIR$/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/src/main/resources" charset="UTF-8" />
</component>
</project>

14
.idea/misc.xml Normal file
View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_23" default="true" project-jdk-name="openjdk-23" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>

36
pom.xml Normal file
View File

@ -0,0 +1,36 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>derek.guessinggame</groupId>
<artifactId>guessing-game-test</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>23</maven.compiler.source>
<maven.compiler.target>23</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<dependencies>
<dependency><!-- JUnit 5 -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<!-- Enables test discovery -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0</version>
</plugin>
</plugins>
</build>
</project>

View File

@ -0,0 +1,103 @@
package derek.guessinggametest;
import java.util.Scanner;
import java.util.Random;
import java.util.ArrayList;
public class Game {
Scanner in = new Scanner(System.in);
// Method generating a random number ("random") within a specified range ("min" and "max")
public ArrayList<Integer> generateNumberToBeGuessed() {
int min = 1;
int max = 100;
Random randomNumber = new Random();
int random = randomNumber.nextInt(max - min + 1) + min;
ArrayList<Integer> rNumberMinMax = new ArrayList<>();
rNumberMinMax.add(random);
rNumberMinMax.add(min);
rNumberMinMax.add(max);
return rNumberMinMax ;
}
// Method prompting user to guess a number ("guessed") within a specified range
public int makeGuess(int min, int max) {
int guessed = (min - 1);
do {
System.out.print("Enter a number between " + min + " and " + max + ": ");
String input = in.nextLine();
if (input.matches("\\d+") && Integer.parseInt(input) >= min
&& Integer.parseInt(input) <= max) {
guessed = Integer.parseInt(input);
} else {
System.out.println("That entry is invalid.");
}
} while (guessed < min);
return guessed;
}
// Method to check if "guessed" matches "random"
public boolean isCorrectGuess(int random, int guessed) {
return guessed == random;
}
// Method for displaying the game's title
public void displayWelcomeMessage() {
System.out.println(" Welcome to the Number Guessing Game!! ");
System.out.println("~~*~~~*~~~*~~~*~~~*~~~*~~~*~~~*~~~*~~~*~~\n");
}
// Method to display introduction dialogue
public void displayPleaseGuessMessage(int min, int max) {
System.out.println("I'm thinking of a number from " + min + " to " + max + ".");
System.out.println("Try to guess it.\n");
}
// Method to display dialogue if guessed matches random
public void displayCorrectGuessMessage(int counter) {
if (counter <= 3) {
System.out.println("Great work! You are a mathematical wizard.");
} else if (counter > 3 && counter < 7) {
System.out.println("Not too bad! You've got potential.\n");
} else if (counter >= 7) {
System.out.println("What took you so long?\n");
}
}
// Method to display dialogue if guessed != random
public void displayGuessAgainMessage(int random, int guessed) {
if (guessed > (random + 10)) {
System.out.println("Way too high! Guess again: \n");
} else if ((guessed > random) && (guessed <= (random + 10))) {
System.out.println("Too high! Guess again: \n");
} else if (guessed < (random - 10)) {
System.out.println("Way too low! Guess again: \n");
} else if ((guessed < random) && (guessed >= (random - 10))) {
System.out.println("Too low! Guess again: \n");
}
}
// Method prompting user to play again or exit
public boolean displayPlayAgainMessage() {
boolean tryAgain = true;
String input;
do {
System.out.print("Try again? (y/n): ");
input = in.nextLine();
if (input.equalsIgnoreCase("y")) {
tryAgain = true;
} else if (input.equalsIgnoreCase("n")) {
tryAgain = false;
} else {
System.out.println("Enter 'y' for yes or 'n' for no.");
}
} while (!input.equalsIgnoreCase("y") && !input.equalsIgnoreCase("n"));
System.out.println("~~*~~~*~~~*~~~*~~~*~~~*~~~*~~~*~~~*~~~*~~\n");
return tryAgain;
}
}

View File

@ -0,0 +1,46 @@
package derek.guessinggametest;
import java.util.Scanner;
import java.util.ArrayList;
public class GuessingNumberApp {
Scanner in = new Scanner(System.in);
public static void main(String[] args) {
// Instance of Game class
Game game = new Game();
// Display welcome message
game.displayWelcomeMessage();
// Create main loop that terminates when user enters "n" at "tryAgain" prompt
do {
ArrayList<Integer> rNumberMinMax = game.generateNumberToBeGuessed();
// Unpack "rNumber" to use "random", "min", and "max" throughout application
int random = rNumberMinMax.get(0);
int min = rNumberMinMax.get(1);
int max = rNumberMinMax.get(2);
int counter = 0;
boolean correctGuess = false;
// Introduction dialogue
game.displayPleaseGuessMessage(min, max);
// Inner loop that continues until the "random" is guessed
do {
int guessed = game.makeGuess(min, max);
counter++;
correctGuess = game.isCorrectGuess(rNumberMinMax.get(0), guessed);
if (correctGuess){
// "tryOrTries" enables the use of the word 'try' in the output if "random" is guessed on first attempt
String tryOrTries = (counter == 1) ? "try" : "tries";
System.out.println("You got it in " + counter + " " + tryOrTries + ".");
game.displayCorrectGuessMessage(counter);
break;
} else {
game.displayGuessAgainMessage(random, guessed);
}
} while (!correctGuess);
// Loops back if user enters "y" or exits if user enters "n"
} while (game.displayPlayAgainMessage());
System.out.println("Bye!! Come back soon!!");
}
}

View File

@ -0,0 +1,45 @@
package derek.guessinggametest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.*;
public class GameTest {
private Game game;
@BeforeEach
void instantiateGame() {
game = new Game();
}
@Test
@DisplayName("Test that min, max, and random are correct")
public void testGenerateNumberToBeGuessed() {
ArrayList<Integer> result = game.generateNumberToBeGuessed();
assertEquals(3, result.size());
int random = result.get(0);
int min = result.get(1);
int max = result.get(2);
assertEquals(1, min);
assertEquals(100, max);
assertTrue(random >= min && random <= max);
}
@Test
@DisplayName("Test if same numbers return true")
public void testIsCorrectGuess() {
assertTrue(game.isCorrectGuess(3,3));
}
@Test
@DisplayName("Different Numbers return false")
public void testIsCorrectGuessFalse() {
assertFalse(game.isCorrectGuess(3,4));
}
}

View File

@ -0,0 +1,4 @@
package derek.guessinggametest;
public class GuessingNumberAppTest {
}