Changed HandTest to use more formal mocking.

This commit is contained in:
neviyn 2017-01-22 15:02:16 +00:00
parent 60fe77f044
commit 021886be4d
2 changed files with 50 additions and 138 deletions

View File

@ -10,6 +10,7 @@ mainClassName = "uk.co.neviyn.pokergame.App"
repositories { repositories {
mavenCentral() mavenCentral()
jcenter()
} }
tasks.withType(JavaCompile) { tasks.withType(JavaCompile) {
@ -20,6 +21,7 @@ dependencies {
compile 'org.slf4j:slf4j-api:1.7.22' compile 'org.slf4j:slf4j-api:1.7.22'
compile 'org.slf4j:slf4j-simple:1.7.22' compile 'org.slf4j:slf4j-simple:1.7.22'
testCompile group: 'junit', name: 'junit', version: '4.11' testCompile group: 'junit', name: 'junit', version: '4.11'
testCompile "org.mockito:mockito-core:2.+"
} }
test { test {

View File

@ -1,9 +1,10 @@
package game; package game;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; 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.Hand;
import uk.co.neviyn.pokergame.game.IDeck; import uk.co.neviyn.pokergame.game.IDeck;
import uk.co.neviyn.pokergame.model.Card; 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.Suit;
import uk.co.neviyn.pokergame.model.Value; import uk.co.neviyn.pokergame.model.Value;
import java.util.Stack;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class HandTest { public class HandTest {
private FakeDeck deck = new FakeDeck(); @Mock IDeck deck;
@Before
public void setUp() throws Exception {
deck.clearDeck();
}
@Test @Test
public void royalStraightFlush() { public void royalStraightFlush() {
deck.addCard(new Card(Suit.CLUBS, Value.TEN)); when(deck.drawCard()).thenReturn(new Card(Suit.CLUBS, Value.TEN), new Card(Suit.CLUBS, Value.JACK),
deck.addCard(new Card(Suit.CLUBS, Value.JACK)); new Card(Suit.CLUBS, Value.QUEEN), new Card(Suit.CLUBS, Value.KING), new Card(Suit.CLUBS, Value.ACE));
deck.addCard(new Card(Suit.CLUBS, Value.QUEEN));
deck.addCard(new Card(Suit.CLUBS, Value.KING));
deck.addCard(new Card(Suit.CLUBS, Value.ACE));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
Assert.assertEquals(Result.ROYAL_STRAIGHT_FLUSH, hand.checkForWin()); Assert.assertEquals(Result.ROYAL_STRAIGHT_FLUSH, hand.checkForWin());
} }
@Test @Test
public void royalStraightFlushWithJoker() { public void royalStraightFlushWithJoker() {
deck.addCard(new Card(Suit.CLUBS, Value.TEN)); when(deck.drawCard()).thenReturn(new Card(Suit.CLUBS, Value.TEN), new Card(Suit.CLUBS, Value.JACK),
deck.addCard(new Card(Suit.CLUBS, Value.JACK)); new Card(Suit.JOKER, Value.JOKER), new Card(Suit.CLUBS, Value.KING), new Card(Suit.CLUBS, Value.ACE));
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
deck.addCard(new Card(Suit.CLUBS, Value.KING));
deck.addCard(new Card(Suit.CLUBS, Value.ACE));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.ROYAL_STRAIGHT_FLUSH, hand.checkForWin()); assertEquals(Result.ROYAL_STRAIGHT_FLUSH, hand.checkForWin());
} }
@Test @Test
public void fiveOfAKind() { public void fiveOfAKind() {
deck.addCard(new Card(Suit.CLUBS, Value.TEN)); when(deck.drawCard()).thenReturn(new Card(Suit.CLUBS, Value.TEN), new Card(Suit.SPADES, Value.TEN),
deck.addCard(new Card(Suit.SPADES, Value.TEN)); new Card(Suit.HEARTS, Value.TEN), new Card(Suit.DIAMONDS, Value.TEN), new Card(Suit.JOKER, Value.JOKER));
deck.addCard(new Card(Suit.HEARTS, Value.TEN));
deck.addCard(new Card(Suit.DIAMONDS, Value.TEN));
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.FIVE_OF_A_KIND, hand.checkForWin()); assertEquals(Result.FIVE_OF_A_KIND, hand.checkForWin());
} }
@Test @Test
public void straightFlush() { public void straightFlush() {
deck.addCard(new Card(Suit.SPADES, Value.TWO)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.THREE),
deck.addCard(new Card(Suit.SPADES, Value.THREE)); new Card(Suit.SPADES, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.SPADES, Value.SIX));
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
deck.addCard(new Card(Suit.SPADES, Value.SIX));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin()); assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin());
} }
@Test @Test
public void straightFlushWithJoker() { public void straightFlushWithJoker() {
deck.addCard(new Card(Suit.SPADES, Value.TWO)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.THREE),
deck.addCard(new Card(Suit.SPADES, Value.THREE)); new Card(Suit.JOKER, Value.JOKER), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.SPADES, Value.SIX));
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
deck.addCard(new Card(Suit.SPADES, Value.SIX));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin()); assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin());
deck.addCard(new Card(Suit.JOKER, Value.JOKER)); when(deck.drawCard()).thenReturn(new Card(Suit.JOKER, Value.JOKER), new Card(Suit.SPADES, Value.THREE),
deck.addCard(new Card(Suit.SPADES, Value.THREE)); new Card(Suit.SPADES, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.SPADES, Value.SIX));
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
deck.addCard(new Card(Suit.SPADES, Value.SIX));
hand.newRound(); hand.newRound();
assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin()); assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin());
deck.addCard(new Card(Suit.SPADES, Value.TWO)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.THREE),
deck.addCard(new Card(Suit.SPADES, Value.THREE)); new Card(Suit.SPADES, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.JOKER, Value.JOKER));
deck.addCard(new Card(Suit.SPADES, Value.FOUR));
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
hand.newRound(); hand.newRound();
assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin()); assertEquals(Result.STRAIGHT_FLUSH, hand.checkForWin());
} }
@Test @Test
public void fourOfAKind() { public void fourOfAKind() {
deck.addCard(new Card(Suit.SPADES, Value.TWO)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.HEARTS, Value.TWO),
deck.addCard(new Card(Suit.HEARTS, Value.TWO)); new Card(Suit.DIAMONDS, Value.TWO), new Card(Suit.CLUBS, Value.TWO), new Card(Suit.SPADES, Value.SIX));
deck.addCard(new Card(Suit.DIAMONDS, Value.TWO));
deck.addCard(new Card(Suit.CLUBS, Value.TWO));
deck.addCard(new Card(Suit.SPADES, Value.SIX));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.FOUR_OF_A_KIND, hand.checkForWin()); assertEquals(Result.FOUR_OF_A_KIND, hand.checkForWin());
} }
@Test @Test
public void fourOfAKindWithJoker() { public void fourOfAKindWithJoker() {
deck.addCard(new Card(Suit.SPADES, Value.TWO)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.HEARTS, Value.TWO),
deck.addCard(new Card(Suit.HEARTS, Value.TWO)); new Card(Suit.JOKER, Value.JOKER), new Card(Suit.CLUBS, Value.TWO), new Card(Suit.SPADES, Value.SIX));
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
deck.addCard(new Card(Suit.CLUBS, Value.TWO));
deck.addCard(new Card(Suit.SPADES, Value.SIX));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.FOUR_OF_A_KIND, hand.checkForWin()); assertEquals(Result.FOUR_OF_A_KIND, hand.checkForWin());
} }
@Test @Test
public void fullHouse() { public void fullHouse() {
deck.addCard(new Card(Suit.SPADES, Value.FOUR)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR),
deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); new Card(Suit.DIAMONDS, Value.FOUR), new Card(Suit.CLUBS, Value.JACK), new Card(Suit.SPADES, Value.JACK));
deck.addCard(new Card(Suit.DIAMONDS, Value.FOUR));
deck.addCard(new Card(Suit.CLUBS, Value.JACK));
deck.addCard(new Card(Suit.SPADES, Value.JACK));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.FULL_HOUSE, hand.checkForWin()); assertEquals(Result.FULL_HOUSE, hand.checkForWin());
} }
@Test @Test
public void flush() { public void flush() {
deck.addCard(new Card(Suit.SPADES, Value.TWO)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.FOUR),
deck.addCard(new Card(Suit.SPADES, Value.FOUR)); new Card(Suit.SPADES, Value.ACE), new Card(Suit.SPADES, Value.NINE), new Card(Suit.SPADES, Value.KING));
deck.addCard(new Card(Suit.SPADES, Value.ACE));
deck.addCard(new Card(Suit.SPADES, Value.NINE));
deck.addCard(new Card(Suit.SPADES, Value.KING));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.FLUSH, hand.checkForWin()); assertEquals(Result.FLUSH, hand.checkForWin());
} }
@Test @Test
public void flushWithJoker() { public void flushWithJoker() {
deck.addCard(new Card(Suit.SPADES, Value.TWO)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.SPADES, Value.FOUR),
deck.addCard(new Card(Suit.SPADES, Value.FOUR)); new Card(Suit.JOKER, Value.JOKER), new Card(Suit.SPADES, Value.NINE), new Card(Suit.SPADES, Value.KING));
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
deck.addCard(new Card(Suit.SPADES, Value.NINE));
deck.addCard(new Card(Suit.SPADES, Value.KING));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.FLUSH, hand.checkForWin()); assertEquals(Result.FLUSH, hand.checkForWin());
} }
@Test @Test
public void straight() { public void straight() {
deck.addCard(new Card(Suit.SPADES, Value.TWO)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.DIAMONDS, Value.THREE),
deck.addCard(new Card(Suit.DIAMONDS, Value.THREE)); new Card(Suit.HEARTS, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.CLUBS, Value.SIX));
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
deck.addCard(new Card(Suit.CLUBS, Value.SIX));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.STRAIGHT, hand.checkForWin()); assertEquals(Result.STRAIGHT, hand.checkForWin());
} }
@Test @Test
public void nearStraight() { public void nearStraight() {
deck.addCard(new Card(Suit.SPADES, Value.THREE)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.THREE), new Card(Suit.DIAMONDS, Value.THREE),
deck.addCard(new Card(Suit.DIAMONDS, Value.THREE)); new Card(Suit.HEARTS, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.CLUBS, Value.SIX));
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
deck.addCard(new Card(Suit.CLUBS, Value.SIX));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.ONE_PAIR, hand.checkForWin()); assertEquals(Result.ONE_PAIR, hand.checkForWin());
} }
@Test @Test
public void straightWithJoker() { public void straightWithJoker() {
deck.addCard(new Card(Suit.SPADES, Value.TWO)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.DIAMONDS, Value.THREE),
deck.addCard(new Card(Suit.DIAMONDS, Value.THREE)); new Card(Suit.JOKER, Value.JOKER), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.CLUBS, Value.SIX));
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
deck.addCard(new Card(Suit.CLUBS, Value.SIX));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.STRAIGHT, hand.checkForWin()); assertEquals(Result.STRAIGHT, hand.checkForWin());
deck.addCard(new Card(Suit.JOKER, Value.JOKER)); when(deck.drawCard()).thenReturn(new Card(Suit.JOKER, Value.JOKER), new Card(Suit.DIAMONDS, Value.THREE),
deck.addCard(new Card(Suit.DIAMONDS, Value.THREE)); new Card(Suit.HEARTS, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.CLUBS, Value.SIX));
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
deck.addCard(new Card(Suit.CLUBS, Value.SIX));
hand.newRound(); hand.newRound();
assertEquals(Result.STRAIGHT, hand.checkForWin()); assertEquals(Result.STRAIGHT, hand.checkForWin());
deck.addCard(new Card(Suit.SPADES, Value.TWO)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.TWO), new Card(Suit.DIAMONDS, Value.THREE),
deck.addCard(new Card(Suit.DIAMONDS, Value.THREE)); new Card(Suit.HEARTS, Value.FOUR), new Card(Suit.SPADES, Value.FIVE), new Card(Suit.JOKER, Value.JOKER));
deck.addCard(new Card(Suit.HEARTS, Value.FOUR));
deck.addCard(new Card(Suit.SPADES, Value.FIVE));
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
hand.newRound(); hand.newRound();
assertEquals(Result.STRAIGHT, hand.checkForWin()); assertEquals(Result.STRAIGHT, hand.checkForWin());
} }
@Test @Test
public void threeOfAKind() { public void threeOfAKind() {
deck.addCard(new Card(Suit.SPADES, Value.FOUR)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR),
deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); new Card(Suit.DIAMONDS, Value.FOUR), new Card(Suit.CLUBS, Value.THREE), new Card(Suit.SPADES, Value.ACE));
deck.addCard(new Card(Suit.DIAMONDS, Value.FOUR));
deck.addCard(new Card(Suit.CLUBS, Value.THREE));
deck.addCard(new Card(Suit.SPADES, Value.ACE));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.THREE_OF_A_KIND, hand.checkForWin()); assertEquals(Result.THREE_OF_A_KIND, hand.checkForWin());
} }
@Test @Test
public void threeOfAKindWithJoker() { public void threeOfAKindWithJoker() {
deck.addCard(new Card(Suit.SPADES, Value.FOUR)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR),
deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); new Card(Suit.JOKER, Value.JOKER), new Card(Suit.CLUBS, Value.THREE), new Card(Suit.SPADES, Value.ACE));
deck.addCard(new Card(Suit.JOKER, Value.JOKER));
deck.addCard(new Card(Suit.CLUBS, Value.THREE));
deck.addCard(new Card(Suit.SPADES, Value.ACE));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.THREE_OF_A_KIND, hand.checkForWin()); assertEquals(Result.THREE_OF_A_KIND, hand.checkForWin());
} }
@Test @Test
public void twoPair() { public void twoPair() {
deck.addCard(new Card(Suit.SPADES, Value.FOUR)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR),
deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); new Card(Suit.DIAMONDS, Value.TEN), new Card(Suit.CLUBS, Value.ACE), new Card(Suit.SPADES, Value.ACE));
deck.addCard(new Card(Suit.DIAMONDS, Value.TEN));
deck.addCard(new Card(Suit.CLUBS, Value.ACE));
deck.addCard(new Card(Suit.SPADES, Value.ACE));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.TWO_PAIR, hand.checkForWin()); assertEquals(Result.TWO_PAIR, hand.checkForWin());
} }
@Test @Test
public void onePair() { public void onePair() {
deck.addCard(new Card(Suit.SPADES, Value.FOUR)); when(deck.drawCard()).thenReturn(new Card(Suit.SPADES, Value.FOUR), new Card(Suit.HEARTS, Value.FOUR),
deck.addCard(new Card(Suit.HEARTS, Value.FOUR)); new Card(Suit.DIAMONDS, Value.TEN), new Card(Suit.CLUBS, Value.SIX), new Card(Suit.SPADES, Value.ACE));
deck.addCard(new Card(Suit.DIAMONDS, Value.TEN));
deck.addCard(new Card(Suit.CLUBS, Value.SIX));
deck.addCard(new Card(Suit.SPADES, Value.ACE));
Hand hand = new Hand(deck); Hand hand = new Hand(deck);
assertEquals(Result.ONE_PAIR, hand.checkForWin()); assertEquals(Result.ONE_PAIR, hand.checkForWin());
} }
private class FakeDeck implements IDeck {
private Stack<Card> 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<>();
}
}
} }