Java:什么是基本的/最基本的JUnit测试?
问题 什么是基本的/最基本的JUnit测试?是否有一个最佳实践或配方,对于刚刚进入TDD/单元测试的人来说是理想的 背景Java:什么是基本的/最基本的JUnit测试?,java,unit-testing,junit,tdd,Java,Unit Testing,Junit,Tdd,问题 什么是基本的/最基本的JUnit测试?是否有一个最佳实践或配方,对于刚刚进入TDD/单元测试的人来说是理想的 背景 package com.craigreedwilliams.game; /** * Created by Reed on 7/10/2015. */ public class Card { private String rank; private String suit; private int rankInt; // TODO: re
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;
}
}
我对TDD和单元测试感兴趣。试图更好地理解它以及如何使用它。我一直在研究一些例子&它们帮了我很大的忙。我目前正在尝试将单元测试转换到我自己的工作中&我正在寻找池的浅端、基础和基本要素
我的问题如上所述,下面有一个应用程序示例,我将尝试在其中实现一些测试。如果您还知道任何您认为对初学者来说非常适合了解测试的时间和原因的资源,请与我们分享
示例
我已经包括了我感兴趣的3个Java类(Card、Deck、Wallet)。此外,我还包括了3个Java测试类(CardTest、DeckTest、WalletTest),它们基本上是空的,除了package、imports、class和我正在考虑的注释掉的测试方法。如果你能给我举一些非常有用的例子
卡片
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;
}
}
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;
// }
}
包com.craigreedwillams
/**
* 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;
// }
}
甲板测试
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;
}
}
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;
// }
}
代码注释:您可能希望将
Rank
s和Suit
s实现为枚举。到“基本测试”。。。所有的测试都同样重要。虫子就是虫子。不管多小,都可能造成大破坏。这就是代码覆盖工具存在的原因。如果您开始测试驱动的开发,那么您首先要定义某些合同,例如“一个人不能从钱包中存入负数的钱”。这些测试独立于您的实现。稍后,您可以使用代码覆盖率工具检查哪些行已测试,哪些行未测试,并设计新的测试以提高测试覆盖率。@Turing85所有测试都同样重要。bug就是bug。
这不是真的。丢失数据或阻止软件使用的bug要比错误显示数据的bug重要得多。@MartinCarney,这要视情况而定。想象一下,在您的医疗应用程序视图中,您切换了一些输出,但是由于这个错误,某人没有得到他/她需要的器官。您可以检查代码中是否存在逻辑错误,但如果使用应用程序的用户必须根据您的观点做出决定,则此错误可能更致命。这本书对于学习TDD非常有用。@Turing85很好的反例。这种最坏的情况正是我可能永远不会使用健康相关软件的原因。