Question
What are the basic/most essential JUnit test? Is there a best practice or recipe, that would be ideal for someone just getting into TDD/Unit Testing?
Background
I'm interested in TDD & unit test. Attempting to better understand it & how to use it. I've been working through some examples & they have been a lot of help. I'm currently trying to transition unit testing into my own work & am looking for the shallow-end of the pool, the basics, the essentials.
My question is stated above and I have an example below of an app I will try to implement some test in. If you also know of any resources that you think are great for beginners looking to understand the when & why of test, please share.
Example
I've include 3 Java classes(Card, Deck, Wallet) that I'm interested in test. Additionally, Ive included 3 Java test classes(CardTest, DeckTest, WalletTest) which are essentially empty, except for package, imports, class, and the commented-out methods I am thinking about testing. If you could provide me with some examples that would be very helpful.
Card
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;
}
}
Deck
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];
}
}
Wallet
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];
// }
}
Wallet Test
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;
// }
}
Aucun commentaire:
Enregistrer un commentaire