JAVAJava学习笔记Java学习笔记

JUnit使用总结

2017-06-19  本文已影响473人  大华夏

被测试的类

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);
    }
}
上一篇下一篇

猜你喜欢

热点阅读