JUnit使用總結

weixin_33766168發表於2017-06-19

被測試的類

public class Calculator {
    public int add(int one, int another) {
        // 為了簡單起見,暫不考慮溢位等情況。
        return one + another;
    }
    public int multiply(int one, int another) {
        // 為了簡單起見,暫不考慮溢位等情況。
        return one * another;
    }
}

1.使用和不使用之間的差別

1.1 不使用

        /**
     * 不使用Junit進行測試
     * @author YW0941
     *
     */
    public static void main(String[] args) {
        Calculator calculator = new Calculator();
        int sum = calculator.add(1, 2);
        if (sum == 3) {
            System.out.println("add() works!");
        } else {
            System.out.println("add() does not works!");
        }
        int product = calculator.multiply(2, 4);
        if (product == 8) {
            System.out.println("multiply() works!");
        } else {
            System.out.println("multiply() does not works!");
        }
    }

1.2 使用JUnit

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class CalculatorTest {
    @Test
    public void testAdd() throws Exception {
        System.out.println("testAdd  running ......");
        Calculator calculator = new Calculator();
        int sum = calculator.add(1, 2);
        Assert.assertEquals(3, sum);
        
    }
    @Test
    public void testMultiply() throws Exception {
        System.out.println("testMultiply  running ......");
        Calculator calculator = new Calculator();
        int product = calculator.multiply(2, 4);
        Assert.assertEquals(8, product);
    }
}

2.Assert類常用方法

assertEquals(expected, actual)
assertEquals(expected, actual, tolerance)
assertTrue(boolean condition)
assertFalse(boolean condition)
assertNull(Object obj)
assertNotNull(Object obj)
assertSame(expected, actual)
assertNotSame(expected, actual)
fail()
備註:上面方法都有一個過載的方法,方法第一個引數是一個字串,結果報導里會顯示此字串引數值,讓測試結果更具可讀性,更清楚出錯原因

3.幾個註解

@Test
@Before
@After
@BeforeClass
@AfterClass

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
public class CalculatorTest {
    
    private Calculator calculator; 
    @Before
    public void setup(){
        
        calculator = new Calculator();
        System.out.println("每個測試方法執行之前都會被呼叫......");
    }
    @After
    public void release(){
        System.out.println("每個測試方法執行之後都會被呼叫......");
    }
    
    @BeforeClass
    public static void loadClass(){
//      calculator = new Calculator();
        System.out.println("跑此測試類所有方法前呼叫......");
    }
    @AfterClass
    public static void releaseClass(){
        System.out.println("跑此測試類所有方法之後呼叫......");
    }
    
    @Test
    public void testAdd() throws Exception {
        System.out.println("testAdd  running ......");
//      Calculator calculator = new Calculator();
        int sum = calculator.add(1, 2);
        Assert.assertEquals(3, sum);
        
    }
    @Test
    public void testMultiply() throws Exception {
        System.out.println("testMultiply  running ......");
//        Calculator calculator = new Calculator();
        int product = calculator.multiply(2, 4);
        Assert.assertEquals(8, product);
    }
}