其他分享
首页 > 其他分享> > junit4总结

junit4总结

作者:互联网

一、JUnit的流程

@BeforeClass:在所有用例执行前执行一次,要用static修饰
@AfterClass:在所有用例执行后执行一次,要用static修饰
@Before:在每个用例执行前执行,不需要用static修饰
@After:在每个用例执行后执行,不需要用static修饰
示例:
public class JunitDemoTests {
    @BeforeClass
     public static void setUpBeforeClass() throws Exception {
           System.out.println("this is before class");
    }

    @AfterClass
    public static void tearDownAfterClass() throws Exception {
        System.out.println("this is after  class");
    }


    @Before
    public  void setUp() throws Exception {
        System.out.println("this is before");
    }

    @After
    public  void tearDown() throws Exception {
        System.out.println("this is aDown");
    }

    @Test
    public void test1() {
           System.out.println("this is test1");
    }

    @Test
    public void test2() {
        System.out.println("this is test2");
    }

}

二、测试套件

有三个文件,TaskTest1.java、TaskTest2.java、TaskTest.java,在TaskTest.java中引入TaskTest1和TaskTest2的测试用例

示例:

TaskTest1.java、TaskTest2.java

import org.junit.Test;

public class TaskTest1 {
    @Test
     public void test() {
            System.out.println("this is TaskTest1");
             }
}
import org.junit.Test;


public class TaskTest2 {
    @Test
     public void test() {
            System.out.println("this is TaskTest2");
             }
}

TaskTest.java

import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({ TaskTest1.class, TaskTest2.class })
public class TaskTest {

}

执行结果:

 三、参数化

  Calculate.java

public class Calculate {
    public int sum(int first, int second) {
        return first + second;
    }
}
ParametersTest.java
import java.util.Arrays;
import java.util.Collection;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

@RunWith(Parameterized.class)
public class ParametersTest
{
    private int expected;
    private int first;
    private int second;

    private Calculate calculate = new Calculate();

    public ParametersTest(int expected, int first, int second) {
        super();
        this.expected = expected;
        this.first = first;
        this.second = second;
    }

    @Parameters
    public static Collection<Integer[]> testDatas() {
        // 测试参数:和构造函数中参数的位置一一对应。
        return Arrays.asList(new Integer[][] { { 3, 2, 1 }, { 5, 3, 2 },
                { 7, 6, 1 } });
    }

    @Test
    public void testSum() {
        System.out.println("测试:" + first + "+" + second);
        Assert.assertEquals(expected, calculate.sum(first, second));
    }

}

 四、注解

@Ignore:不执行此用例

    @Ignore
    @Test
    public void test2() {
        System.out.println("this is test2");
    }

@Test(expected = ArithmeticException.class):异常测试,预期是此用例会报错,执行此异常用例时会通过

    @Test(expected = ArithmeticException.class)
    public void test3() {
        System.out.println("in test case 3");
        int a = 0;
        int b = 1 / a;
    }

 执行结果是用例通过

 

 

 

标签:总结,void,System,class,println,Test,junit4,public
来源: https://www.cnblogs.com/jina1121/p/15724610.html