包类设计:
Calculator类设计:
package addTest.junit;
public class Calculator {
private static int result;
public void add(int n) { result = result + n; }
public void substract(int n) { result = result - 1;}//Bug: result =result-n
public void multiply(int n) { }
public void divide(int n) { result = result / n; }
public void square(int n) { result = n * n; }
public void squareRoot(int n) { for (; ;) ;}//Bug:dead loop
public void clear() { result = 0; }
public int getResult(){ return result;}
}
AtMain类设计:
package addTest.junit;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
//In one project, write a test class is not possible, we will write a lot of test classes. But these tests must be an execution and trouble.
//In view of this, we should do 'packaging test': test all need to run together
@RunWith(Suite.class)
@Suite.SuiteClasses({ ATSub1.class, ATSub2.class })
public class AtMain {
@BeforeClass
public static void doBeforeClass(){
System.out.println("AtMain.doBeforeClass()");
}
@Before
public void before() throws Exception {
System.out.println("AtMain.before()");
}
@After
public void after(){
System.out.println("AtMain.after()");
}
@AfterClass
public static void doAfterClass(){
System.out.println("AtMain.doAfterClass()");
}
}
ATSub1类设计:
package addTest.junit;
import static org.junit.Assert.assertEquals;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
public class ATSub1 {
private static Calculator calculator = new Calculator();
@BeforeClass
public static void doBeforeClass(){
System.out.println("ATSub1.doBeforeClass()");
}
@Before
public void setUp() throws Exception {
System.out.println("ATSub1.setUp()");
calculator.clear();
}
@After
public void after(){
System.out.println("AtSub1.after()");
}
@AfterClass
public static void doAfterClass(){
System.out.println("AtSub1.doAfterClass()");
}
@Test
public void testAdd() {
calculator.add(2);
calculator.add(3);
assertEquals(5, calculator.getResult());
}
@Test
public void testSubstract() {
calculator.add(10);
calculator.substract(2);
assertEquals(8, calculator.getResult());
}
@Ignore("Multiply() Not yet implemented")
@Test
public void testMultiply() {
System.out.println("Ingored");
calculator.multiply(0);
}
@Test(timeout = 1000)
public void testsquareRoot() {
calculator.squareRoot(8);
}
@Test(expected = ArithmeticException.class)
public void testDivideZero() {
calculator.divide(0);
}
}
ATSub2类设计:
package addTest.junit;
import static org.junit.Assert.assertEquals;
import java.util.Arrays;
import java.util.Collection;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
//Some times, We should test this function that parameters have many special value, or that arguments are divided into many areas.
//For example, an evaluation of the score, the return value is "excellent, good, general, pass, fail," --- then your write 5 test at least.-- it is a very trouble.
@RunWith(Parameterized.class)
public class ATSub2 {
private static Calculator calculator = new Calculator();
private int param;
private int result;
@Parameters
public static Collection<Object[]> data(){
return Arrays.asList(new Object[][]{{2, 4},{0, 0},{-3, 11}, });
}
public ATSub2(int param, int result){
this.param = param;
this.result = result;
}
@Test
public void square(){
calculator.square(param);
assertEquals(result, calculator.getResult());
}
@BeforeClass
public static void doBeforeClass(){
System.out.println("ATSub2.doBeforeClass()");
}
@Before
public void setUp() throws Exception {
System.out.println("ATSub2.setUp()");
calculator.clear();
}
@After
public void after(){
System.out.println("ATSub2.after()");
}
@AfterClass
public static void doAfterClass(){
System.out.println("ATSub2.doAfterClass()");
}
}