Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can you make a classes for these tests. 1---------------- import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.*; class

Can you make a classes for these tests.

1----------------

import org.hamcrest.Matchers; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.*;

class NumberGuessingGameTest { private NumberGuessingGame game;

@BeforeEach void initGame() { game = new NumberGuessingGame(); }

@Test void play() { final Player player1; final Player player2; final Player player3;

player1 = new ComputerPlayer("Player 1", new SmallestNumberGuesser()); player2 = new ComputerPlayer("Player 2", new LargestNumberGuesser()); player3 = new ComputerPlayer("Player 3", new BestNumberGuesser());

play(1, 10, 10, player1, player1); play(1, 10, 1, player2, player2); play(1, 10, 2, player3, player3); play(1, 5, 3, player3, player1, player2, player3); play(1, 5, 2, player1, player1, player2, player3); play(1, 10, 9, player2, player1, player2, player3); }

private void play(final int min, final int max, final int numberToGuess, final Player expectedWinner, final Player ... players) { final Player winner;

winner = game.play(min, max, numberToGuess, players);

assertThat(winner.getName(), equalTo(expectedWinner.getName())); }

@Test void badPlay() { final Player player1; final Player player2; final Player player3;

player1 = new ComputerPlayer("Player 1", new SmallestNumberGuesser()); player2 = new ComputerPlayer("Player 2", new LargestNumberGuesser()); player3 = new ComputerPlayer("Player 3", new BestNumberGuesser());

// min < max badPlay(100, 99, 10, "min (100) must be < max (99)"); badPlay(2, 1, 10, "min (2) must be < max (1)"); badPlay(100, 1, 10, "min (100) must be < max (1)");

// number to guess < min badPlay(1, 1, 0, "numberToGuess (0) must be >= min (1)"); badPlay(10, 20, 0, "numberToGuess (0) must be >= min (10)"); badPlay(10, 20, 9, "numberToGuess (9) must be >= min (10)");

// number to guess > max badPlay(1, 1, 2, "numberToGuess (2) must be <= max (1)"); badPlay(10, 20, 21, "numberToGuess (21) must be <= max (20)"); badPlay(10, 20, 200, "numberToGuess (200) must be <= max (20)");

// players badPlay(1, 1, 1, "players cannot be null", null); badPlay(1, 1, 1, "players cannot be empty", new Player[]{}); badPlay(1, 1, 1, "players[0] cannot be null", new Player[] { null }); badPlay(1, 1, 1, "players[1] cannot be null", new Player[] { player1, null }); badPlay(1, 1, 1, "players[2] cannot be null", new Player[] { player1, player2, null }); }

private void badPlay(final int min, final int max, final int numberToGuess, final String expectedMessage, final Player ... players) { Throwable ex;

ex = assertThrows(IllegalArgumentException.class, () -> { game.play(min, max, numberToGuess, players); } ); assertThat(ex.getMessage(), Matchers.equalTo(expectedMessage)); } }

2------------------

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.*;

class LargestNumberGuesserTest extends NumberGuesserTest { @Test void guess() { final LargestNumberGuesser guesser1; final LargestNumberGuesser guesser2;

guesser1 = new LargestNumberGuesser(); guesser2 = new LargestNumberGuesser();

assertThat(guesser1.guess(0, 1000), equalTo(1000)); assertThat(guesser2.guess(0, 1000), equalTo(1000)); assertThat(guesser1.guess(1, 1000), equalTo(1000)); assertThat(guesser2.guess(0, 1000), equalTo(1000)); assertThat(guesser1.guess(2, 1000), equalTo(1000)); assertThat(guesser2.guess(999, 1000), equalTo(1000)); assertThat(guesser1.guess(1000, 1000), equalTo(1000)); assertThat(guesser2.guess(1000, 1000), equalTo(1000)); assertThat(guesser1.guess(1, 1), equalTo(1)); assertThat(guesser2.guess(1, 2), equalTo(2)); assertThat(guesser1.guess(100, 102), equalTo(102)); assertThat(guesser2.guess(100, 103), equalTo(103)); }

@Test void badGuess() { badGuess(new LargestNumberGuesser()); } }

3------------------------

import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.*;

class SmallestNumberGuesserTest extends NumberGuesserTest { @Test void guess() { final SmallestNumberGuesser guesser1; final SmallestNumberGuesser guesser2;

guesser1 = new SmallestNumberGuesser(); guesser2 = new SmallestNumberGuesser();

assertThat(guesser1.guess(0, 1000), equalTo(0)); assertThat(guesser2.guess(0, 1000), equalTo(0)); assertThat(guesser1.guess(1, 1000), equalTo(1)); assertThat(guesser2.guess(0, 1000), equalTo(0)); assertThat(guesser1.guess(2, 1000), equalTo(2)); assertThat(guesser2.guess(999, 1000), equalTo(999)); assertThat(guesser1.guess(1000, 1000), equalTo(1000)); assertThat(guesser2.guess(1000, 1000), equalTo(1000)); assertThat(guesser1.guess(1, 1), equalTo(1)); assertThat(guesser2.guess(1, 2), equalTo(1)); }

@Test void badGuess() { badGuess(new SmallestNumberGuesser()); } }

4--------------------

import org.hamcrest.Matchers; import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.*;

class BestNumberGuesserTest extends NumberGuesserTest { @Test void guess() { final BestNumberGuesser guesser1; final BestNumberGuesser guesser2;

guesser1 = new BestNumberGuesser(); guesser2 = new BestNumberGuesser();

assertThat(guesser1.guess(1, 1), equalTo(1)); assertThat(guesser1.guess(1, 2), equalTo(1)); assertThat(guesser1.guess(1, 3), equalTo(2)); assertThat(guesser2.guess(1, 4), equalTo(2)); assertThat(guesser1.guess(1, 5), equalTo(3)); assertThat(guesser1.guess(1, 6), equalTo(3)); assertThat(guesser2.guess(1, 7), equalTo(4)); assertThat(guesser1.guess(2, 4), equalTo(3)); assertThat(guesser2.guess(1, 100), equalTo(50)); assertThat(guesser2.guess(100, 200), equalTo(150)); assertThat(guesser2.guess(1, 10000123), equalTo(5000062)); }

@Test void badGuess() { badGuess(new BestNumberGuesser()); } }

5------------

import org.hamcrest.Matchers; import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.equalTo; import static org.junit.jupiter.api.Assertions.*;

class ComputerPlayerTest { @Test void badConstructor() { badConstructor(null, null, "nm cannot be null"); badConstructor("", null, "nm cannot be blank"); badConstructor(" ", null, "nm cannot be blank"); badConstructor("\t", null, "nm cannot be blank"); badConstructor(" ", null, "nm cannot be blank"); badConstructor(" ", null, "nm cannot be blank"); badConstructor("x", null, "g cannot be null"); badConstructor("X", null, "g cannot be null"); }

private void badConstructor(final String name, final NumberGuesser guesser, final String expectedMessage) { Throwable ex;

ex = assertThrows(IllegalArgumentException.class, () -> { new ComputerPlayer(name, guesser); } ); assertThat(ex.getMessage(), Matchers.equalTo(expectedMessage)); }

6---------------

import org.hamcrest.Matchers;

import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows;

public abstract class NumberGuesserTest { protected void badGuess(final NumberGuesser guesser) { badGuess(guesser, 1, 0, "min (1) must be <= max (0)"); badGuess(guesser, 100, 1, "min (100) must be <= max (1)"); badGuess(guesser, 100, 99, "min (100) must be <= max (99)"); }

private void badGuess(final NumberGuesser guesser, final int min, final int max, final String expectedMessage) { Throwable ex;

ex = assertThrows(IllegalArgumentException.class, () -> { guesser.guess(min, max); } ); assertThat(ex.getMessage(), Matchers.equalTo(expectedMessage)); } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Machine Performance Modeling Methodologies And Evaluation Strategies Lncs 257

Authors: Francesca Cesarini ,Silvio Salza

1st Edition

3540179429, 978-3540179429

More Books

Students also viewed these Databases questions