Java test on unit 3 doesn't work.

Asked 2 years ago, Updated 2 years ago, 407 views

I created a CardDeck class, and I had a task to test it with unit 3.However, eight of the 12 methods tested fail (one appears to be hiding a failure, so it's actually nine).The CardDeck class itself works fine, so I think there is a problem with the test method, but I don't know, so please let me know (I'm a beginner at programming).Sorry for the poor code.)
Incidentally, there are other Card classes that treat cards as 0 spades, 1 diamonds, 2 hearts, and 3 clover, and CardDeck classes treat cards in Card classes as decks.

↓CardDeck class

import java.util.Collections;
import java.util.List;

/**
 * a card deck class 
 * 
 * @author  
 */

public class CardDeck {
    /** Card deck, list to register cards*/
    private ArrayList<Card>cards=new ArrayList<>();
    
    /**Create an empty instance*/
    publicCardDeck(){
        
    }
    
    /**Methods for creating a full deck*/
    public void createFullDeck() {
        inti,j;
        for(i=0;i<4;i++){
            for(j=1;j<14;j++){
                Card card = new Card(i,j);
                addCard (card);
            }
        }
    }
    
    /** Method to add joker*/
    public void addJoker() {
        Card card = new Card (-1,0);
        addCard (card);
    }
    
    /** Method of emptying deck*/
    public void clear() {
        cards.clear();
    }
    
    /** Method of shuffling deck cards*/
    public void shuffle() {
        Collections.shuffle (cards);
    }
    
    /** Method of adding a card to the end of the deck*/
    public void addCard (Cardcard) {
        cards.add(card);
    }
    
    /** Method of adding a card to the specified number */
    public void addCard(inti,Cardcard){
        cards.add(i-1,card);
    }
    
    /** Method of retrieving and returning the first card */
    publicCard takeCard(){
        Card top = cards.get(0);
        cards.remove(0);
        return top;
    }
    
    /** Method of retrieving and returning the card with the specified number */
    publicCard takeCard(inti){
        Card trump = cards.get(i-1);
        cards.remove(i-1);
        return Trump;
    }
    
    /** Method of returning card information for the specified number location */
    publicCard seeCard(inti){
        Card trump = cards.get(i-1);
        return Trump;
    }
    
    /** Method of returning the specified number of cards on the deck*/
    public int searchCard(int suite, int number) {
        Card card = new Card (suit, number);
        int num;
        if(cards.contains(card)){
            num = cards.indexOf(card)+1;
        } else {
            num = 0;
        }
        return num;
    }
    
    /** Method for checking deck availability*/
    public boolean isEmpty(){
        boolean empty=cards.isEmpty();
        return empty;
    }
    
    /**Method of returning the number of cards on the deck*/
    public int size() {
        int size = cards.size();
        return size;
    }
    
    /** Method for displaying all deck card information*/
    public void showAllCards() {
        ArrayList<Integer>suit=new ArrayList<>();
        ArrayList<Integer>number = new ArrayList<>();
        inti;
        Card card;
        for(i=0;i<cards.size();i++){
            card = cards.get(i);
            suit.add(card.getSuit());
            number.add(card.getNumber());
        }
        
        System.out.println("---------------------------------------------------------------------------);
        intj;
        String trump;
        for(j=0;j<suit.size();j++){
            System.out.print(j+1+"th card:");
            Trump = Card.getString(suit.get(j), number.get(j));
            System.out.println(trump);
        }
        System.out.println("--------------------------------------");
    }
    /** Method of returning deck card information*/
    publicList<Card>getAllCards(){
        return cards;
    }
    
    /**
     * acquire a deck
     * 
     * @return deck
     */
    publicArrayList<Card>getCards(){
        return cards;
    }

    /**
     * set up a deck
     * 
     * @param cards
     *            deck
     */
    public void setCards (ArrayList<Card>cards) {
        this.cards = cards;
    }
    
}

↓Card class

**
 * a card class with pictures, numbers, 
 * 
 * @author 
 */

public class card {
    
    /** Design*/
    private int suite;
    /** US>Number*/
    private int number;
    
    /** Empty Constructors*/
    publicCard(){
        
    }
    
    /** Create an instance by specifying patterns and numbers*/
    publicCard(int suite, int number) {
        this.suit =suit;     
        This.number = number;
    }
    
    /**
     * acquire a design
     * 
     * @return design
     */
    public int getSuit(){
        return suit;
    }
    
    /**
     * get a number
     * 
     * @return number
     */
    public int getNumber() {
        return number;
    }
    
    /**
     * method for returning an integer representation of a card
     */
    public static intgetIndex(int suite, int number) {
        int serialnumber = 0;
        switch(suit){
        case0:serialnumber=number-1;break;
        case1 —serialnumber=number+12;break;
        case2 —serialnumber=number+25;break;
        case3 —serialnumber=number+38;break;
        case-1 —serialnumber=-1;break;
        }
        return serial number;
    }
    
    /**
     * method for returning a card string expression
     */
    public static String getString(int suite, int number) {
        String stringsuit = null, stringnumber = null, stringtrump;
        switch(suit){
        case0:
            stringsuit = "Spade";
            break;
        case1:
            stringsuit = "Diamonds";
            break;
        case2:
            stringsuit = "Heart";
            break;
        case3:
            stringsuit="Club";
            break;
        case-1:
            stringsuit = "Joker";
            break;
        }
        
        if(number==1){
            stringnumber = "A";
        }
        if(number==2){
            stringnumber = "2";
        }
        if(number==3){
            stringnumber ="3";
        }
        if(number==4){
            stringnumber = "4";
        }
        if(number==5){
            stringnumber = "5";
        }
        if(number==6){
            stringnumber = "6";
        }
        if(number==7){
            stringnumber = "7";
        }
        if(number==8){
            stringnumber = "8";
        }
        if(number==9){
            stringnumber = "9";
        }
        if(number==10){
            stringnumber = "10";
        }
        if(number==11){
            stringnumber = "J";
        }
        if(number==12){
            stringnumber = "Q";
        }
        if(number==13){
            stringnumber = "K";
        }
        if(number==0){
            stringnumber="";
        }
        
        stringtrump = stringsuit + stringnumber;
        
        return stringtrump;
    }
    
    /**
     * a method of converting card information into an integer display 
     */
    public int to Index() {
        int serialnumber = getIndex(suit, number);
        return serial number;
    }
    
    /**
     *  a method of converting card information into a string display 
     */
    @ Override
    public String to String() {
        String stringtrump = getString(suit, number);
        return stringtrump;
    }
    
    /**
     *  a method of outputting card information to a screen
     */
    public void show() {
        System.out.println(toString());
    }
    
}

↓test class

import unit.framework.TestCase;

/** Card Deck Class Test Class*/
public class CardDeckTest extensions TestCase{
    // DECK INSTANCE FOR ALL TEST METHODS
    private CardDeck deck, deck1, deck2, deckJ, deck4, deckEmpty;
    // Instances of cards used in all test methods
    private card spadeA, diamond10, heartQ, clubK, joker;

    /**
     * Pre-processing of each test method execution
     */
    protected void setUp()throws Exception {
        // I'll make some test card instances.
        spadeA = new Card(0,1); 
        diamond10 = new Card(1,10); 
        heartQ = new Card(2,12); 
        clubK = new Card(3,13);
        joker = new Card (-1,0);
        
        // I've made some deck instances for testing.
        deckEmpty = new CardDeck();
        deck = new CardDeck();
        deck1 = new CardDeck();
        deck2 = new CardDeck();
        deckJ = new CardDeck();
        deck4 = new CardDeck();
        
        deck1.addCard(spadeA);
        deck1.addCard (diamond10);
        deck1.addCard (heartQ);
        deck1.addCard (clubK);
        
        deck2.addCard (spadeA);
        deck2.addCard (diamond10);
        deck2.addCard (clubK);
        
        deckJ.addCard (joker);
        
        deck4.addCard(spadeA);
        deck4.addCard (diamond10);
        deck4.addCard (heartQ);
        deck4.addCard (clubK);
        
        for(inti=0;i<4;i++){
            for(int j=1;j<14;j++){
                Card card = new Card(i,j);
                deck.addCard(card);
            }
        }
        
    }

    protected void tearDown()throws Exception {
        
    }
    
    public void testCreateFullDeck(){
        deckEmpty.createFullDeck();
        assertEquals(deck, deckEmpty);
    }
    
    public void testAddJoker() {
        deckEmpty.addJoker();
        assertEquals(deckJ, deckEmpty);
    }
    
    public void testClear() {
        deck1.clear();
        assertTrue(deckEmpty.equals(deck1));
    }

    public void testShuffle(){
        deck4.shuffle();
        assertNotSame(deck1,deck4);
    }

    public void testAddCard(){
        deckEmpty.addCard(joker);
        assertTrue(deckJ.equals(deckEmpty));
    }

    public void testAddCardIntCard(){
        deck2.addCard(3,heartQ);
        assertEquals (deck1, deck2);
    }

    public void testTakeCard(){
        assertEquals(deckEmpty, deckJ.takeCard());
    }

    public void testTakeCardInt(){
        assertEquals(deck2,deck1.takeCard(3));
    }

    public void testSeeCard(){
        assertEquals(joker, deckJ.seeCard(1));
        assertEquals (diamond10, deck1.seeCard(2));
    }

    public void testSerchCard(){
        assertEquals (1, deck1.serchCard(0,1));
        
    }

    public void testIsEmpty(){
        assertEquals(true, deckEmpty.isEmpty());
        assertEquals(false, deck1.isEmpty());
    }

    public void testSize(){
        assertEquals(4,deck1.size());
    }

}

java junit

2022-09-30 22:04

1 Answers

Eight of the 12 methods tested fail (one is actually 9 because it seems to be hidden).

I didn't know which was referring to (all of them fail when I run the question code), but the question code is incorrect by two points:
You can go through the test by correcting them.

1.
The test code evaluates CardDeck and Card with equals (assertEquals), so you must override equals (and hashCode) for each class.

The following code was automatically generated by the Eclipse feature:

public class CardDeck{

    // ...

    @ Override
    public int hashCode(){
        return Objects.hash(cards);
    }

    @ Override
    public boolean equals (Object obj) {
        if(this==obj)
            return true;
        if(obj==null)
            return false;
        if(getClass()!=obj.getClass())
            return false;
        CardDeck other=(CardDeck)obj;
        return Objects.equals (cards, other.cards);
    }
}
public classCard{

    // ...

    @ Override
    public int hashCode(){
        return Objects.hash(number, suite);
    }

    @ Override
    public boolean equals (Object obj) {
        if(this==obj)
            return true;
        if(obj==null)
            return false;
        if(getClass()!=obj.getClass())
            return false;
        Card other=(Card)obj;
        return number==other.number&&suit==other.suit;
    }

}

testTakeCard compares the deckEmpty (type CardDeck) with the deckJ.takeCard() return value (type Card), so the test always fails.
I think what I want to compare here is not the joker card, but the deck from which the joker card was extracted, so the test code is as follows:

public void testTakeCard(){
        // extract one from a deck containing only joker
        deckJ.takeCard();
        // Compare to empty deck
        assertEquals (deckEmpty, deckJ);
    }

testTakeCardInt is the same:

public void testTakeCardInt(){
        deck1.takeCard(3);
        assertEquals(deck2,deck1);
    }


2022-09-30 22:04

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.