From 021886be4dbd4beda354a04d07c5d4dc3bd741f8 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Sun, 22 Jan 2017 15:02:16 +0000 Subject: [PATCH] Changed HandTest to use more formal mocking. --- build.gradle | 2 + src/test/java/game/HandTest.java | 186 ++++++++----------------------- 2 files changed, 50 insertions(+), 138 deletions(-) diff --git a/build.gradle b/build.gradle index b0b7f67..b3b20a7 100644 --- a/build.gradle +++ b/build.gradle @@ -10,6 +10,7 @@ mainClassName = "uk.co.neviyn.pokergame.App" repositories { mavenCentral() + jcenter() } tasks.withType(JavaCompile) { @@ -20,6 +21,7 @@ dependencies { compile 'org.slf4j:slf4j-api:1.7.22' compile 'org.slf4j:slf4j-simple:1.7.22' testCompile group: 'junit', name: 'junit', version: '4.11' + testCompile "org.mockito:mockito-core:2.+" } test { diff --git a/src/test/java/game/HandTest.java b/src/test/java/game/HandTest.java index 75a7528..b33ecaa 100644 --- a/src/test/java/game/HandTest.java +++ b/src/test/java/game/HandTest.java @@ -1,9 +1,10 @@ package game; import org.junit.Assert; -import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; +import org.junit.runner.RunWith; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnitRunner; import uk.co.neviyn.pokergame.game.Hand; import uk.co.neviyn.pokergame.game.IDeck; import uk.co.neviyn.pokergame.model.Card; @@ -11,254 +12,163 @@ import uk.co.neviyn.pokergame.model.Result; import uk.co.neviyn.pokergame.model.Suit; import uk.co.neviyn.pokergame.model.Value; -import java.util.Stack; - import static org.junit.Assert.*; +import static org.mockito.Mockito.*; +@RunWith(MockitoJUnitRunner.class) public class HandTest { - private FakeDeck deck = new FakeDeck(); - - @Before - public void setUp() throws Exception { - deck.clearDeck(); - } + @Mock IDeck deck; @Test public void royalStraightFlush() { - deck.addCard(new Card(Suit.CLUBS, Value.TEN)); - deck.addCard(new Card(Suit.CLUBS, Value.JACK)); - deck.addCard(new Card(Suit.CLUBS, Value.QUEEN)); - deck.addCard(new Card(Suit.CLUBS, Value.KING)); - deck.addCard(new Card(Suit.CLUBS, Value.ACE)); + when(deck.drawCard()).thenReturn(new Card(Suit.CLUBS, Value.TEN), new Card(Suit.CLUBS, Value.JACK), + new Card(Suit.CLUBS, Value.QUEEN), new Card(Suit.CLUBS, Value.KING), new Card(Suit.CLUBS, Value.ACE)); Hand hand = new Hand(deck); Assert.assertEquals(Result.ROYAL_STRAIGHT_FLUSH, hand.checkForWin()); } @Test public void royalStraightFlushWithJoker() { - deck.addCard(new Card(Suit.CLUBS, Value.TEN)); - deck.addCard(new Card(Suit.CLUBS, Value.JACK)); - deck.addCard(new Card(Suit.JOKER, Value.JOKER)); - deck.addCard(new Card(Suit.CLUBS, Value.KING)); - deck.addCard(new Card(Suit.CLUBS, Value.ACE)); + when(deck.drawCard()).thenReturn(new Card(Suit.CLUBS, Value.TEN), new Card(Suit.CLUBS, Value.JACK), + new Card(Suit.JOKER, Value.JOKER), new Card(Suit.CLUBS, Value.KING), new Card(Suit.CLUBS, Value.ACE)); Hand hand = new Hand(deck); assertEquals(Result.ROYAL_STRAIGHT_FLUSH, hand.checkForWin()); } @Test public void fiveOfAKind() { - deck.addCard(new Card(Suit.CLUBS, Value.TEN)); - deck.addCard(new Card(Suit.SPADES, Value.TEN)); - deck.addCard(new Card(Suit.HEARTS, Value.TEN)); - deck.addCard(new Card(Suit.DIAMONDS, Value.TEN)); - deck.addCard(new Card(Suit.JOKER, Value.JOKER)); + when(deck.drawCard()).thenReturn(new Card(Suit.CLUBS, Value.TEN), new Card(Suit.SPADES, Value.TEN), + new Card(Suit.HEARTS, Value.TEN), new Card(Suit.DIAMONDS, Value.TEN), new Card(Suit.JOKER, Value.JOKER)); Hand hand = new Hand(deck); assertEquals(Result.FIVE_OF_A_KIND, hand.checkForWin()); } @Test public void straightFlush() { - deck.addCard(new Card(Suit.SPADES, Value.TWO)); - deck.addCard(new Card(Suit.SPADES, Value.THREE)); - deck.addCard(new Card(Suit.SPADES, Value.FOUR)); - deck.addCard(new Card(Suit.SPADES, Value.FIVE)); - deck.addCard(new Card(Suit.SPADES, Value.SIX)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.THREE), + new Card(Suit.SPADES, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.SPADES, Value.SIX)); Hand hand = new Hand(deck); assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin()); } @Test public void straightFlushWithJoker() { - deck.addCard(new Card(Suit.SPADES, Value.TWO)); - deck.addCard(new Card(Suit.SPADES, Value.THREE)); - deck.addCard(new Card(Suit.JOKER, Value.JOKER)); - deck.addCard(new Card(Suit.SPADES, Value.FIVE)); - deck.addCard(new Card(Suit.SPADES, Value.SIX)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.THREE), + new Card(Suit.JOKER, Value.JOKER), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.SPADES, Value.SIX)); Hand hand = new Hand(deck); assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin()); - deck.addCard(new Card(Suit.JOKER, Value.JOKER)); - deck.addCard(new Card(Suit.SPADES, Value.THREE)); - deck.addCard(new Card(Suit.SPADES, Value.FOUR)); - deck.addCard(new Card(Suit.SPADES, Value.FIVE)); - deck.addCard(new Card(Suit.SPADES, Value.SIX)); + when(deck.drawCard()).thenReturn(new Card(Suit.JOKER, Value.JOKER), new Card(Suit.SPADES, Value.THREE), + new Card(Suit.SPADES, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.SPADES, Value.SIX)); hand.newRound(); assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin()); - deck.addCard(new Card(Suit.SPADES, Value.TWO)); - deck.addCard(new Card(Suit.SPADES, Value.THREE)); - deck.addCard(new Card(Suit.SPADES, Value.FOUR)); - deck.addCard(new Card(Suit.SPADES, Value.FIVE)); - deck.addCard(new Card(Suit.JOKER, Value.JOKER)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.THREE), + new Card(Suit.SPADES, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.JOKER, Value.JOKER)); hand.newRound(); assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin()); } @Test public void fourOfAKind() { - deck.addCard(new Card(Suit.SPADES, Value.TWO)); - deck.addCard(new Card(Suit.HEARTS, Value.TWO)); - deck.addCard(new Card(Suit.DIAMONDS, Value.TWO)); - deck.addCard(new Card(Suit.CLUBS, Value.TWO)); - deck.addCard(new Card(Suit.SPADES, Value.SIX)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.HEARTS, Value.TWO), + new Card(Suit.DIAMONDS, Value.TWO), new Card(Suit.CLUBS, Value.TWO), new Card(Suit.SPADES, Value.SIX)); Hand hand = new Hand(deck); assertEquals(Result.FOUR_OF_A_KIND, hand.checkForWin()); } @Test public void fourOfAKindWithJoker() { - deck.addCard(new Card(Suit.SPADES, Value.TWO)); - deck.addCard(new Card(Suit.HEARTS, Value.TWO)); - deck.addCard(new Card(Suit.JOKER, Value.JOKER)); - deck.addCard(new Card(Suit.CLUBS, Value.TWO)); - deck.addCard(new Card(Suit.SPADES, Value.SIX)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.HEARTS, Value.TWO), + new Card(Suit.JOKER, Value.JOKER), new Card(Suit.CLUBS, Value.TWO), new Card(Suit.SPADES, Value.SIX)); Hand hand = new Hand(deck); assertEquals(Result.FOUR_OF_A_KIND, hand.checkForWin()); } @Test public void fullHouse() { - deck.addCard(new Card(Suit.SPADES, Value.FOUR)); - deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); - deck.addCard(new Card(Suit.DIAMONDS, Value.FOUR)); - deck.addCard(new Card(Suit.CLUBS, Value.JACK)); - deck.addCard(new Card(Suit.SPADES, Value.JACK)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR), + new Card(Suit.DIAMONDS, Value.FOUR), new Card(Suit.CLUBS, Value.JACK), new Card(Suit.SPADES, Value.JACK)); Hand hand = new Hand(deck); assertEquals(Result.FULL_HOUSE, hand.checkForWin()); } @Test public void flush() { - deck.addCard(new Card(Suit.SPADES, Value.TWO)); - deck.addCard(new Card(Suit.SPADES, Value.FOUR)); - deck.addCard(new Card(Suit.SPADES, Value.ACE)); - deck.addCard(new Card(Suit.SPADES, Value.NINE)); - deck.addCard(new Card(Suit.SPADES, Value.KING)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.FOUR), + new Card(Suit.SPADES, Value.ACE), new Card(Suit.SPADES, Value.NINE), new Card(Suit.SPADES, Value.KING)); Hand hand = new Hand(deck); assertEquals(Result.FLUSH, hand.checkForWin()); } @Test public void flushWithJoker() { - deck.addCard(new Card(Suit.SPADES, Value.TWO)); - deck.addCard(new Card(Suit.SPADES, Value.FOUR)); - deck.addCard(new Card(Suit.JOKER, Value.JOKER)); - deck.addCard(new Card(Suit.SPADES, Value.NINE)); - deck.addCard(new Card(Suit.SPADES, Value.KING)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.FOUR), + new Card(Suit.JOKER, Value.JOKER), new Card(Suit.SPADES, Value.NINE), new Card(Suit.SPADES, Value.KING)); Hand hand = new Hand(deck); assertEquals(Result.FLUSH, hand.checkForWin()); } @Test public void straight() { - deck.addCard(new Card(Suit.SPADES, Value.TWO)); - deck.addCard(new Card(Suit.DIAMONDS, Value.THREE)); - deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); - deck.addCard(new Card(Suit.SPADES, Value.FIVE)); - deck.addCard(new Card(Suit.CLUBS, Value.SIX)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.DIAMONDS, Value.THREE), + new Card(Suit.HEARTS, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.CLUBS, Value.SIX)); Hand hand = new Hand(deck); assertEquals(Result.STRAIGHT, hand.checkForWin()); } @Test public void nearStraight() { - deck.addCard(new Card(Suit.SPADES, Value.THREE)); - deck.addCard(new Card(Suit.DIAMONDS, Value.THREE)); - deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); - deck.addCard(new Card(Suit.SPADES, Value.FIVE)); - deck.addCard(new Card(Suit.CLUBS, Value.SIX)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.THREE), new Card(Suit.DIAMONDS, Value.THREE), + new Card(Suit.HEARTS, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.CLUBS, Value.SIX)); Hand hand = new Hand(deck); assertEquals(Result.ONE_PAIR, hand.checkForWin()); } @Test public void straightWithJoker() { - deck.addCard(new Card(Suit.SPADES, Value.TWO)); - deck.addCard(new Card(Suit.DIAMONDS, Value.THREE)); - deck.addCard(new Card(Suit.JOKER, Value.JOKER)); - deck.addCard(new Card(Suit.SPADES, Value.FIVE)); - deck.addCard(new Card(Suit.CLUBS, Value.SIX)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.DIAMONDS, Value.THREE), + new Card(Suit.JOKER, Value.JOKER), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.CLUBS, Value.SIX)); Hand hand = new Hand(deck); assertEquals(Result.STRAIGHT, hand.checkForWin()); - deck.addCard(new Card(Suit.JOKER, Value.JOKER)); - deck.addCard(new Card(Suit.DIAMONDS, Value.THREE)); - deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); - deck.addCard(new Card(Suit.SPADES, Value.FIVE)); - deck.addCard(new Card(Suit.CLUBS, Value.SIX)); + when(deck.drawCard()).thenReturn(new Card(Suit.JOKER, Value.JOKER), new Card(Suit.DIAMONDS, Value.THREE), + new Card(Suit.HEARTS, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.CLUBS, Value.SIX)); hand.newRound(); assertEquals(Result.STRAIGHT, hand.checkForWin()); - deck.addCard(new Card(Suit.SPADES, Value.TWO)); - deck.addCard(new Card(Suit.DIAMONDS, Value.THREE)); - deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); - deck.addCard(new Card(Suit.SPADES, Value.FIVE)); - deck.addCard(new Card(Suit.JOKER, Value.JOKER)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.DIAMONDS, Value.THREE), + new Card(Suit.HEARTS, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.JOKER, Value.JOKER)); hand.newRound(); assertEquals(Result.STRAIGHT, hand.checkForWin()); } @Test public void threeOfAKind() { - deck.addCard(new Card(Suit.SPADES, Value.FOUR)); - deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); - deck.addCard(new Card(Suit.DIAMONDS, Value.FOUR)); - deck.addCard(new Card(Suit.CLUBS, Value.THREE)); - deck.addCard(new Card(Suit.SPADES, Value.ACE)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR), + new Card(Suit.DIAMONDS, Value.FOUR), new Card(Suit.CLUBS, Value.THREE), new Card(Suit.SPADES, Value.ACE)); Hand hand = new Hand(deck); assertEquals(Result.THREE_OF_A_KIND, hand.checkForWin()); } @Test public void threeOfAKindWithJoker() { - deck.addCard(new Card(Suit.SPADES, Value.FOUR)); - deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); - deck.addCard(new Card(Suit.JOKER, Value.JOKER)); - deck.addCard(new Card(Suit.CLUBS, Value.THREE)); - deck.addCard(new Card(Suit.SPADES, Value.ACE)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR), + new Card(Suit.JOKER, Value.JOKER), new Card(Suit.CLUBS, Value.THREE), new Card(Suit.SPADES, Value.ACE)); Hand hand = new Hand(deck); assertEquals(Result.THREE_OF_A_KIND, hand.checkForWin()); } @Test public void twoPair() { - deck.addCard(new Card(Suit.SPADES, Value.FOUR)); - deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); - deck.addCard(new Card(Suit.DIAMONDS, Value.TEN)); - deck.addCard(new Card(Suit.CLUBS, Value.ACE)); - deck.addCard(new Card(Suit.SPADES, Value.ACE)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR), + new Card(Suit.DIAMONDS, Value.TEN), new Card(Suit.CLUBS, Value.ACE), new Card(Suit.SPADES, Value.ACE)); Hand hand = new Hand(deck); assertEquals(Result.TWO_PAIR, hand.checkForWin()); } @Test public void onePair() { - deck.addCard(new Card(Suit.SPADES, Value.FOUR)); - deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); - deck.addCard(new Card(Suit.DIAMONDS, Value.TEN)); - deck.addCard(new Card(Suit.CLUBS, Value.SIX)); - deck.addCard(new Card(Suit.SPADES, Value.ACE)); + when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR), + new Card(Suit.DIAMONDS, Value.TEN), new Card(Suit.CLUBS, Value.SIX), new Card(Suit.SPADES, Value.ACE)); Hand hand = new Hand(deck); assertEquals(Result.ONE_PAIR, hand.checkForWin()); } - - private class FakeDeck implements IDeck { - - private Stack cards = new Stack<>(); - - public void addCard(Card card) { - cards.push(card); - } - - @Override - public Card drawCard() { - return cards.pop(); - } - - @Override - public void resetDeck() { - // FakeDeck has no base deck to reset to. - } - - protected void clearDeck() { - cards = new Stack<>(); - } - } } \ No newline at end of file