四、编写JUnit测试(Writing a Test with JUnit)
编写JUnit测试,只要扩展junit.framework.TestCase类可以了。你的TestCase子类将按你希望的顺序调用test cases,包括可能的测试前设置和测试后清除。设置在setUp方法中进行。清除在tearDown方法中进行。你可以,但不是必须,重载这两个方法做你想做的事。
下面是对上面的例子用JUnit进行重写的test case:
程序列表3 (CalculatorTest.java, taken from CalculatorTest.java.v2):
import junit.framework.TestCase;
public class CalculatorTest extends TestCase {
private FactorCalculator calc;
public CalculatorTest(String name) {
super(name);
}
protected void setUp() {
calc = new FactorCalculator();
}
public void testFactor() {
int numToFactor;
int[] factorArray;
int[] correctFactorArray;
numToFactor = 100;
factorArray = calc.factor(numToFactor);
correctFactorArray = new int[] {2, 2, 5, 5};
assertTrue("bad factorization of " + numToFactor, isSameFactorArray(factorArray, correctFactorArray));
numToFactor = 4;
factorArray = calc.factor(numToFactor);
correctFactorArray = new int[] {2, 2};
assertTrue("bad factorization of " + numToFactor, isSameFactorArray(factorArray, correctFactorArray));
numToFactor = 3;
factorArray = calc.factor(numToFactor);
correctFactorArray = new int[] {3};
assertTrue("bad factorization of " + numToFactor, isSameFactorArray(factorArray, correctFactorArray));
numToFactor = 2;
factorArray = calc.factor(numToFactor);
correctFactorArray = new int[] {2};
assertTrue("bad factorization of " + numToFactor, isSameFactorArray(factorArray, correctFactorArray));
}
// presumes both factor arrays are in numeric order
private boolean isSameFactorArray(int[] factorArray1, int[] factorArray2) {
boolean isSame = false;
if (factorArray1.length == factorArray2.length) {
isSame = true;
for(int i = 0; i < factorArray1.length; i++) {
if (factorArray1[i] != factorArray2[i]) {
isSame = false;
break;
}
}
}
return isSame;
}
public void testIsPrime() {
int numToCheck;
boolean isPrime;
numToCheck = 2;
isPrime = calc.isPrime(numToCheck);
assertTrue("bad isPrime value for " + numToCheck, isPrime);
numToCheck = 3;
isPrime = calc.isPrime(numToCheck);
assertTrue("bad isPrime value for " + numToCheck, isPrime);
numToCheck = 4;
isPrime = calc.isPrime(numToCheck);
assertFalse("bad isPrime value for " + numToCheck, isPrime);
try {
numToCheck = 1;
isPrime = calc.isPrime(numToCheck);
fail("isPrime should throw exception for numbers less than 2");
} catch (IllegalArgumentException e) {
// do nothing because throwing IAE is the proper action
}
}
public void testIsDivisor() {
int numToCheck;
int potentialDivisor;
boolean isDivisor;
numToCheck = 6;
potentialDivisor = 3;
isDivisor = calc.isDivisor(numToCheck, potentialDivisor);
assertTrue("bad isDivisor value for (" + numToCheck + ", " + potentialDivisor + ")", isDivisor);
numToCheck = 5;
potentialDivisor = 2;
isDivisor = calc.isDivisor(numToCheck, potentialDivisor);
assertFalse("bad isDivisor value for (" + numToCheck + ", " + potentialDivisor + ")", isDivisor);
try {
numToCheck = 6;
potentialDivisor = 0;
isDivisor = calc.isDivisor(numToCheck, potentialDivisor);
fail("isDivisor should throw an exception when potentialDivisor is 0 but did not");
} catch (ArithmeticException e) {
// do nothing because throwing AE is the proper action
}
}
}