-3

So basically I'm trying to test to see if my hand of 5 cards has a pair in it (two cards have the same value (1-9)) and I'm getting an unknown error and this is my code

Error:

java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86)...

Code:

public static boolean hasPair(Card[] cards) {
    Deck theDeck = new Deck();
    cards = theDeck.deal(5);

    int k=0;

    for (int atPos = 0; atPos<5; atPos++){ 
        for (int atPos2 = atPos+1; atPos2<5; atPos2++){ 

            if(cards[atPos].getValue() == cards[atPos2].getValue()){ 
                k++;
            } 
        }
    }
    if (k==2){ 
        return true; 
    }
    else { 
        return false; 
    }

JUnit that is failing

@Test
public void testExampleTest_SinglePairTest() {
    Card[] testHand = new Card[5];
    testHand[0] = new Card(1,1);
    testHand[1] = new Card(2,1);
    testHand[2] = new Card(2,1);
    testHand[3] = new Card(4,1);
    testHand[4] = new Card(5,1);
    assertTrue(HandEvaluatorBBXP.hasPair(testHand));
masonft
  • 9
  • 5
  • `"...I'm getting an unknown error"` -- a what? Your error is fully unknown to us if you don't post the text of the error message -- why not do this? – Hovercraft Full Of Eels Nov 21 '16 at 22:35
  • java.lang.AssertionError at org.junit.Assert.fail(Assert.java:86)... I don't know what the error is because I'm submitting it to a submit server. – masonft Nov 21 '16 at 22:36
  • a) Are we playing poker or blackjack? b) what if there are 2 pair? c) a dumb compiler would say 'not all paths return a value' – John3136 Nov 21 '16 at 22:39
  • *"AssertionError at org.junit.Assert.fail("* that's not your program you ran, it's you UnitTests. How come you have unittests and you don't know it? – Timothy Truckle Nov 21 '16 at 22:40
  • @John3136 it's like blackjack but kind of different b. if there are two pair it'll return false – masonft Nov 21 '16 at 22:49
  • Why not compile and run the code yourself and find your errors **before** submitting it? – Hovercraft Full Of Eels Nov 21 '16 at 22:53
  • what does your Card class contain? – RAZ_Muh_Taz Nov 21 '16 at 23:00
  • @HovercraftFullOfEels my tests compile perfectly is the issue – masonft Nov 21 '16 at 23:03
  • 1
    It's not that the tests don't compile, it's just that whatever it's testing for is not passing the test. At least that's what I can gather from `org.junit.Assert.fail`. Can you post the test class that is failing? – C. Smith Nov 21 '16 at 23:13
  • 1
    Compilation is only a small part of the picture. Well compiled code can still harbor severe logical errors that require debugging to be found, and I suggest that you do just this -- fire up your debugger and run your code through it to check its state as it runs. – Hovercraft Full Of Eels Nov 21 '16 at 23:15
  • @C.Smith I've posted it – masonft Nov 21 '16 at 23:22
  • Fails because `int k =0;` then incrementing `k` once when there is a pair then testing `if (k ==2)` – Come Raczy Nov 22 '16 at 00:14

1 Answers1

0

It seems like there's no point in passing in Card[] cards as an argument to the hasPair method because you immediately set the variable to whatever deck.deal() returns. So the incoming value is never used.

This could well be the reason your test is failing, because your test data what the hasPair method is actually using to evaluate the test with.

C. Smith
  • 411
  • 2
  • 8