Question
Can you make a class to pass these tests. Its unit test you have to create a class to pass these tests import org.hamcrest.Matchers; import
Can you make a class to pass these tests.
Its unit test you have to create a class to pass these tests
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)); }
@Test void makeSmallestGuess() { final ComputerPlayer player1; final ComputerPlayer player2;
player1 = new ComputerPlayer("P1", new SmallestNumberGuesser()); player2 = new ComputerPlayer("P2", new SmallestNumberGuesser());
assertThat(player1.getName(), equalTo("P1")); assertThat(player2.getName(), equalTo("P2"));
for(int i = 0; i < 100; i++) { assertThat(player1.takeTurn(i, 100), equalTo(i)); assertThat(player2.takeTurn(i + 1, 100), equalTo(i + 1)); } }
@Test void makeLargestGuess() { final ComputerPlayer player1; final ComputerPlayer player2;
player1 = new ComputerPlayer("P1", new LargestNumberGuesser()); player2 = new ComputerPlayer("P2", new LargestNumberGuesser());
assertThat(player1.getName(), equalTo("P1")); assertThat(player2.getName(), equalTo("P2"));
for(int i = 100; i > 0; i--) { assertThat(player1.takeTurn(0, i), equalTo(i)); assertThat(player2.takeTurn(0, i - 1), equalTo(i - 1)); } }
@Test void makeBestGuess() { final ComputerPlayer player1; final ComputerPlayer player2;
player1 = new ComputerPlayer("P1", new BestNumberGuesser()); player2 = new ComputerPlayer("P2", new BestNumberGuesser());
assertThat(player1.getName(), equalTo("P1")); assertThat(player2.getName(), equalTo("P2"));
assertThat(player1.takeTurn(0, 10), equalTo(5)); assertThat(player2.takeTurn(1, 10), equalTo(5)); assertThat(player1.takeTurn(2, 100), equalTo(51)); assertThat(player2.takeTurn(2, 96), equalTo(49)); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started