JAVA5新添加的新特性
import static org.junit.Assert.*; import static org.junit.Assert.assertEquals; 例子 package com.hb.demo; import static org.junit.Assert.*; import org.junit.Before; import org.junit.Test; public class CalculatorTest { private static Calculator calculator = new Calculator(); @Before public void setUp() throws Exception { } @Test public void testAdd() { int result = calculator.add(2, 3); org.junit.Assert.assertEquals(3, result); } @Test public void testSub() { int result = calculator.sub(10, 2); org.junit.Assert.assertEquals(8, result); } }结论: @BeforeClass --> @Before --> @Test --> @After --> @AfterClass
如果一个测试用例比起指定的毫秒数花费了更多的时间,那么JUnit将自动将它标记为失败
@Test(timeout = 1000) public void testCase1() throws InterruptedException { TimeUnit.SECONDS.sleep(5000); System.out.println("in test case 1"); }(1)为准备使用参数化测试的测试类指定特殊的运行器org.junit.runners.Parameterized。@RunWith(Parameterized.class)
(2)为测试类声明几个变量,分别用于存放期望值和测试所用数据,期望值可能只有一个,但测试数据变量可能有好几个,比如加法中有两个变量才能得出一个结果。
(3)为测试类声明一个带有参数的公共构造函数,并在其中为第二个环节中声明的几个变量赋值,构造方法是Junit调用的 ☆关键点☆
(4)为测试类声明一个使用注解org.junit.runners.Parameterized.Parameters修饰的,返回值为 java.util.Collection的公共静态方法,并在此方法中初始化所有需要测试的参数对。 ☆关键点☆
(5)编写测试方法,使用定义的变量作为参数进行测试。
package com.hb.demo; import static org.junit.Assert.*; import java.util.Arrays; import java.util.Collection; import org.junit.Before; import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; @RunWith(Parameterized.class) // 第一步:指定特殊的运行器org.junit.runners.Parameterized public class FirstDemoTestParameterization { // 要测试的类 private FirstDemo firstDemo; //第二步:为测试类声明几个变量,分别用于存放期望值和测试所用数据。 private int input1; private boolean expected; @Before // 执行每个测试方法之前都执行一次 public void setUp() throws Exception { System.out.println("我是第三步"); firstDemo = new FirstDemo(); } // 第三步:带有参数的公共构造函数,并在其中为声明的几个变量赋值。 public FirstDemoTestParameterization(int input1, boolean expected) { System.out.println("我是第二步"); this.input1 = input1; // 参数1 this.expected = expected; // 期待的结果值 } //-------------------(1)参数赋值 &&&(2)写出期望值---------------------------- //第四步:为测试类声明一个注解@Parameters,返回值为Collection的公共静态方法,并初始化所有需要测试的参数对。 @Parameters public static Collection prepareData() { System.out.println("我是第一步"); Object[][] object = { { -1, true }, { 13, true } }; // 测试数据 return Arrays.asList(object); // 将数组转换成集合返回 } @Test public void testParameterization() { System.out.println("我是第二步"); //-----------(3)获取实际值&&&(4)断言--比较期望值和实际值。--------------- //第五步:编写测试方法,使用定义的变量作为参数进行测试。 assertEquals(expected, firstDemo.Parameterization(input1)); } } class FirstDemo { public boolean Parameterization(int num) { return num <= 0 ? false : true; } }带有参数的公共构造函数,并在其中为声明的几个变量赋值。
@Before (测试前的准备工作)@Test(实际测试用例)1. 有异常,符合预期
@Test(expected = ArithmeticException.class) publicvoid testCase3() { System.out.println("in test case 3"); int a = 0; int b = 1 / a; }2. 无异常,不符合预期
@Test(expected = ArithmeticException.class) public void testCase3() { System.out.println("in test case 3"); int a = 0; int b = 1 / 1; }会抛出 java.lang.AssertionError: Expected exception: java.lang.ArithmeticException
