1)新建项目,在项目下建libs文件夹
,导入junit-4.13.jar
、hamcrest-core-1.3.jar
,依次 Build Path — Add To Build Path
2)在项目下分别建src资源文件夹
和test资源文件夹
,将测试代码都放在test文件夹下
3)在src文件夹下建包,包下创建.java文件,在.Java文件中写入如下代码
public int add(int a,int b) {
return a + b;
}
4)在test文件夹下建包,包应该和src中的包一致,包下通过junit test case创建测试文件,写入如下代码
package junit4;
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class CalculateTest {
@Test
public void add() {
//assertEquals( 预期值 , 运行的实际值 )
assertEquals(6,new Calculate().add(3,3));
}
}
5)运行
方法1:.java文件 — Run As — 1 JUnit Test,可以运行文件中所有测试类
方法2:直接运行.java文件下的测试类
方法3:选中类,右键 — Run As — 1 JUnit Test
6)结果
在被测试类中写入一个除方法
public int division(int a,int b) {
return a / b;
}
测试类
@Test
public void division() {
//assertEquals( 预期值 , 运行的实际值 )
assertEquals(6,new Calculate().division(3,3));
}
预期结果为6,但实际运行结果为 1,测试失败,这种失败为Failure
还是除法,测试类
@Test
public void division() {
//assertEquals( 预期值 , 运行的实际值 )
assertEquals(6,new Calculate().division(3,0));
}
public class flowTest {
@BeforeClass
public static void setUpBeforeClass() throws Exception {
System.out.println("beforeclass");
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
System.out.println("afterclass");
}
@Before
public void setUp() throws Exception {
System.out.println("before");
}
@After
public void tearDown() throws Exception {
System.out.println("after");
}
@Test
public void test() {
System.out.println("test");
}
@Test
public void test2() {
System.out.println("test2");
}
}
@Test(expected=ArithmeticException.class)
public void division() {
//assertEquals( 预期值 , 运行的实际值 )
//除数为0,Error
assertEquals(6,new Calculate().division(3,0));
}
@Test(timeout=毫秒):限定方法运行时间
当我们不能很好的控制循环代码的终止条件时,为了不让循环变成死循环,使系统崩溃,可以使用timeout限定程序运行时间
测试套件就是组织测试类一起运行
1)创建一个测试套件的入口类,这个类中不包含其它方法
2)更改测试运行器为
Suite.class
@RunWith(Suite.class)
3)将要测试的类作为数组传入@Suite.SuiteClasses({})
@Suite.SuiteClasses({test1.class,test2.class,test3.class})
package junit4;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@RunWith(Suite.class)
@Suite.SuiteClasses({test1.class,test2.class,test3.class})
public class suiteTest {
}
将测试方法的相同结构提取出来,以提高代码的可重用度
1)更改默认的测试运行器为@RunWith(Parameterized.class)
@RunWith(Parameterized.class)
2)声明变量来存放预期值和结果值
public class parameterTest {
int expected = 0;
int input1 = 0;
int input2 = 0;
}
3)声明一个返回值为Collection的公共静态方法,并使用@Parameters进行修饰
@Parameters
public static Collection<Object[]> t(){
return Arrays.asList(new Object[][] {
{3,1,2},
{4,2,2}
});
}
4)为测试类声明一个带有参数的公共构造函数,并在其中为之声明变量赋值
public parameterTest(int expected,int input1,int input2) {
this.expected=expected;
this.input1=input1;
this.input2=input2;
}
5)写一个测试方法使用
@Test
public void testAdd() {
assertEquals(expected,new Calculate().add(input1,input2));
}
所有代码
package junit4;
import static org.junit.Assert.*;
import java.util.Arrays;
import java.util.Collection;
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 parameterTest {
int expected = 0;
int input1 = 0;
int input2 = 0;
@Parameters
public static Collection<Object[]> t(){
return Arrays.asList(new Object[][] {
{3,1,2},
{4,2,2}
});
}
public parameterTest(int expected,int input1,int input2) {
this.expected=expected;
this.input1=input1;
this.input2=input2;
}
@Test
public void testAdd() {
assertEquals(expected,new Calculate().add(input1,input2));
}
}