Advertisement
JeffGrigg

Untitled

May 15th, 2023
2,802
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 7.42 KB | None | 0 0
  1. import org.junit.Test;
  2. import p20230510_TestConsoleIO.GuessTheNumber;
  3. import p20230510_TestConsoleIO.library.MockRandomSingleValue;
  4. import p20230510_TestConsoleIO.library.SystemInputOutputTester;
  5.  
  6. import java.io.IOException;
  7.  
  8. public class GuessTheNumberTest {
  9.  
  10.     @Test
  11.     public void testHighsAndLowsTo33() throws IOException {
  12.  
  13.         final var systemNumberWeAreTryingToGuess = 33;
  14.  
  15.         try (final var mockRandomNumberGenerator = new MockRandomSingleValue(systemNumberWeAreTryingToGuess)) {
  16.             SystemInputOutputTester
  17.                     .whenCallingThisMethod(() -> {
  18.                         GuessTheNumber.main(null);
  19.                     })
  20.                     .assertTextOutput("Welcome to the number guessing program.")
  21.                     //
  22.                     .assertTextOutput("I am thinking of a number in the range of 1 to 100, inclusive.")
  23.                     .assertTextOutput("You need to guess this number, with a minimum number of guesses.")
  24.                     .assertTextOutput("Each time you guess, I will tell you if my number is HIGHER or LOWER than your guess.")
  25.                     //
  26.                     .assertTextOutput("What is your guess?")
  27.                     .provideLineInput("50")
  28.                     .assertTextOutput("Your guess of 50 is TOO HIGH.")
  29.                     //
  30.                     .assertTextOutput("What is your guess?")
  31.                     .provideLineInput("25")
  32.                     .assertTextOutput("Your guess of 25 is TOO LOW.")
  33.                     //
  34.                     .assertTextOutput("What is your guess?")
  35.                     .provideLineInput("37")
  36.                     .assertTextOutput("Your guess of 37 is TOO HIGH.")
  37.                     //
  38.                     .assertTextOutput("What is your guess?")
  39.                     .provideLineInput("31")
  40.                     .assertTextOutput("Your guess of 31 is TOO LOW.")
  41.                     //
  42.                     .assertTextOutput("What is your guess?")
  43.                     .provideLineInput("34")
  44.                     .assertTextOutput("Your guess of 34 is TOO HIGH.")
  45.                     //
  46.                     .assertTextOutput("What is your guess?")
  47.                     .provideLineInput("32")
  48.                     .assertTextOutput("Your guess of 32 is TOO LOW.")
  49.                     //
  50.                     .assertTextOutput("What is your guess?")
  51.                     .provideLineInput("33")
  52.                     .assertTextOutput("Your guess of 33 is CORRECT in 7 guesses!")
  53.                     //
  54.                     .assertThatTheMethodReturnsHere();
  55.         }
  56.     }
  57.  
  58.     @Test
  59.     public void testMaximumGuesses() throws IOException {
  60.  
  61.         final var systemNumberWeAreTryingToGuess = 100;
  62.  
  63.         try (final var mockRandomNumberGenerator = new MockRandomSingleValue(systemNumberWeAreTryingToGuess)) {
  64.             SystemInputOutputTester
  65.                     .whenCallingThisMethod(() -> {
  66.                         GuessTheNumber.main(null);
  67.                     })
  68.                     .assertTextOutput("Welcome to the number guessing program.")
  69.                     //
  70.                     .assertTextOutput("I am thinking of a number in the range of 1 to 100, inclusive.")
  71.                     .assertTextOutput("You need to guess this number, with a minimum number of guesses.")
  72.                     .assertTextOutput("Each time you guess, I will tell you if my number is HIGHER or LOWER than your guess.")
  73.                     //
  74.                     .assertTextOutput("What is your guess?")
  75.                     .provideLineInput("50")
  76.                     .assertTextOutput("Your guess of 50 is TOO LOW.")
  77.                     //
  78.                     .assertTextOutput("What is your guess?")
  79.                     .provideLineInput("75")
  80.                     .assertTextOutput("Your guess of 75 is TOO LOW.")
  81.                     //
  82.                     .assertTextOutput("What is your guess?")
  83.                     .provideLineInput("88")
  84.                     .assertTextOutput("Your guess of 88 is TOO LOW.")
  85.                     //
  86.                     .assertTextOutput("What is your guess?")
  87.                     .provideLineInput("94")
  88.                     .assertTextOutput("Your guess of 94 is TOO LOW.")
  89.                     //
  90.                     .assertTextOutput("What is your guess?")
  91.                     .provideLineInput("97")
  92.                     .assertTextOutput("Your guess of 97 is TOO LOW.")
  93.                     //
  94.                     .assertTextOutput("What is your guess?")
  95.                     .provideLineInput("99")
  96.                     .assertTextOutput("Your guess of 99 is TOO LOW.")
  97.                     //
  98.                     .assertTextOutput("What is your guess?")
  99.                     .provideLineInput("100")
  100.                     .assertTextOutput("Your guess of 100 is CORRECT in 7 guesses!")
  101.                     //
  102.                     .assertThatTheMethodReturnsHere();
  103.         }
  104.     }
  105.  
  106.     @Test
  107.     public void testFirstGuessIsCorrect() throws IOException {
  108.  
  109.         final var systemNumberWeAreTryingToGuess = 24;
  110.  
  111.         try (final var mockRandomNumberGenerator = new MockRandomSingleValue(systemNumberWeAreTryingToGuess)) {
  112.             SystemInputOutputTester
  113.                     .whenCallingThisMethod(() -> {
  114.                         GuessTheNumber.main(null);
  115.                     })
  116.                     .assertTextOutput("Welcome to the number guessing program.")
  117.                     //
  118.                     .assertTextOutput("I am thinking of a number in the range of 1 to 100, inclusive.")
  119.                     .assertTextOutput("You need to guess this number, with a minimum number of guesses.")
  120.                     .assertTextOutput("Each time you guess, I will tell you if my number is HIGHER or LOWER than your guess.")
  121.                     //
  122.                     .assertTextOutput("What is your guess?")
  123.                     .provideLineInput("24")
  124.                     .assertTextOutput("Your guess of 24 is CORRECT in one guess!!!")
  125.                     //
  126.                     .assertThatTheMethodReturnsHere();
  127.         }
  128.     }
  129.  
  130.     @Test
  131.     public void testSecondGuessIsCorrect() throws IOException {
  132.  
  133.         final var systemNumberWeAreTryingToGuess = 33;
  134.  
  135.         try (final var mockRandomNumberGenerator = new MockRandomSingleValue(systemNumberWeAreTryingToGuess)) {
  136.             SystemInputOutputTester
  137.                     .whenCallingThisMethod(() -> {
  138.                         GuessTheNumber.main(null);
  139.                     })
  140.                     .assertTextOutput("Welcome to the number guessing program.")
  141.                     //
  142.                     .assertTextOutput("I am thinking of a number in the range of 1 to 100, inclusive.")
  143.                     .assertTextOutput("You need to guess this number, with a minimum number of guesses.")
  144.                     .assertTextOutput("Each time you guess, I will tell you if my number is HIGHER or LOWER than your guess.")
  145.                     //
  146.                     .assertTextOutput("What is your guess?")
  147.                     .provideLineInput("86")
  148.                     .assertTextOutput("Your guess of 86 is TOO HIGH.")
  149.                     //
  150.                     .assertTextOutput("What is your guess?")
  151.                     .provideLineInput("33")
  152.                     .assertTextOutput("Your guess of 33 is CORRECT in 2 guesses!")
  153.                     //
  154.                     .assertThatTheMethodReturnsHere();
  155.         }
  156.     }
  157.  
  158. }
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement