Java:什么是基本/最重要的JUnit测试?

时间:2015-07-11 19:04:00

标签: java unit-testing junit tdd

问题

什么是基本/最重要的JUnit测试?是否有最佳实践或配方,对于刚刚进入TDD /单元测试的人来说是理想的?

背景

我对TDD&amp ;;感兴趣单元测试。试图更好地理解它&如何使用它。我一直在研究一些例子和他们得到了很多帮助。我正在尝试将单元测试过渡到我自己的工作中。我正在寻找游泳池的浅水区,基础知识,必需品。

我的问题如上所述,我在下面有一个应用程序示例,我将尝试实施一些测试。如果您也知道任何资源,您认为对于初学者来说理解何时和&为什么测试,请分享。

示例

我已经包含了3个我对测试感兴趣的Java类(Card,Deck,Wallet)。此外,我还包括3个Java测试类(CardTest,DeckTest,WalletTest),它们基本上是空的,除了包,导入,类和我正在考虑测试的注释掉的方法。如果你能提供一些非常有帮助的例子。

package com.craigreedwilliams.game;

/**
 * Created by Reed on 7/10/2015.
 */
public class Card {
    private String rank;
    private String suit;
    private int rankInt;

    // TODO: remove this if actually never used
    private int suitInt;

    //four argument constructor initializes Cards rank and suit (stings and ints)
    public Card(String rank, String suit, int rankInt, int suitInt) {
        super();
        this.rank = rank;
        this.suit = suit;
        this.rankInt = rankInt;
        this.suitInt = suitInt;
    }

    //getter method to return the rank value
    public String getRank() {
        return rank;
    }

    //getter method to return the suit value
    public String getSuit() {
        return suit;
    }

    //setter method to initialize the suit
    public int getRankInt() {
        return rankInt;
    }

    //return String representation of Card object
    public String toString() {
        return rank + " : " + suit;
    }
}

甲板

package com.craigreedwilliams.game;

import java.util.Calendar;
import java.util.Random;

/**
 * Created by Reed on 7/10/2015.
 */
public class Deck {
    private final String rank[] = {"2","3","4","5","6","7","8","9","10","Jack","Queen","King", "Ace"};
    private final String suits[]={"Hearts","Diamonds","Clubs","Spades"};

    private final int rankInt[] = {2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14};
    private final int suitsInt[] = {1, 2, 3, 4};

    private Card deck[];
    private final int MAX = 52;
    private Random randNum;

    //makes the deck, constructor - no arguments
    public Deck() {
        deck = new Card[MAX];

        //uses calendar object to get time stamp
        Calendar cal = Calendar.getInstance();
        long seed = cal.getTimeInMillis();

        randNum = new Random(seed); // random generated using time seed

        // uses modulus operator
        for(int i = 0; i < deck.length; i++ ){
            deck[i] = new Card( rank[i % 13], suits[i / 13], rankInt[i % 13], suitsInt[i / 13]);
        }
    }

    //shuffles the deck
    public void shuffle(){

        for(int i = 0; i < deck.length; i++ ){
            int j = randNum.nextInt(MAX);
            Card c = deck[i];
            deck[i] = deck[j];
            deck[j] = c;
        }
    }

    //returns the individual card in the deck
    public Card getCard(int index){
        return deck[index];
    }

    //returns rankInt from the deck object at the given index value
    public int getRankInt(int index) {
        return rankInt[index];
    }

}

钱包

package com.craigreedwilliams.game;

import java.util.Random;

/**
 * Created by Reed on 7/11/2015.
 */
public class Wallet {
    private double balance;
    /**
     * default Wallet constructor
     */
    public Wallet() {
        setRandomStartingBalance(50.0, 500.0);
    }

    private void setRandomStartingBalance(double minimum, double maximum) {
        Random random = new Random();
        double randomStartingBalance = minimum + (maximum - minimum) * random.nextDouble();
        balance = randomStartingBalance;
    }

    public double getBalance() {
        return balance;
    }

    public void deductFromBalance(double price) {
        this.balance = balance - price;
    }
}

CardTest

package com.craigreedwilliams;

/**
 * Created by Reed on 7/11/2015.
 */
public class CardTest {

//    public String getRank() {
//        return rank;
//    }

//    public String getSuit() {
//        return suit;
//    }

//    public int getRankInt() {
//        return rankInt;
//    }

//    //return String representation of Card object
//    public String toString() {
//        return rank + " : " + suit;
//    }
}

DeckTest

package com.craigreedwilliams;

import com.craigreedwilliams.game.Card;

/**
 * Created by Reed on 7/11/2015.
 */
public class DeckTest {


//    public void shuffle(){
//        for(int i = 0; i < deck.length; i++ ){
//            int j = randNum.nextInt(MAX);
//            Card c = deck[i];
//            deck[i] = deck[j];
//            deck[j] = c;
//        }
//    }


//    public Card getCard(int index){
//        return deck[index];
//    }


//    public int getRankInt(int index) {
//        return rankInt[index];
//    }

}

电子钱包测试

package com.craigreedwilliams;

import org.junit.Test;

import java.util.Random;

import static org.junit.Assert.*;

/**
 * Created by Reed on 7/11/2015.
 */
public class WalletTest {

//    private void setRandomStartingBalance(double minimum, double maximum) {
//        Random random = new Random();
//        double randomStartingBalance = minimum + (maximum - minimum) * random.nextDouble();
//        balance = randomStartingBalance;
//    }

//    public double getBalance() {
//        return balance;
//    }

//    public void deductFromBalance(double price) {
//        this.balance = balance - price;
//    }


}

0 个答案:

没有答案
相关问题