本文主要是为了记录很久以前对Junit的回顾。
Junit
Java开发中使用的最多的测试框架,工作中经常会大量使用。
建议遵守约定
- 测试类在test包下(如果是maven结构的项目建议建议不要方在源码中,早期的项目很多是没有区分开的)
- 测试类命名xxxTest结尾。
- 方法命名testxxxx命名。
- 测试方法上必须使用@Test进行修饰。
- 测试方法必须使用public void 进行修饰,不能带任何的参数。
- 测试类的包应该和被测试类保持一致。
- 测试单元中的每个方法必须独立测试,测试方法间不能有任何的依赖。
常用注解
- @Test(expected = ArithmeticException.class) 预期将抛出一个算术异常。
- @Test(timeout = 10) 改方法调用预期的时间范围 10毫秒,若超时算失败。
- @Ignore 被改注解修饰的方法不会被执行。
- @RunWith 可以更改测试运行器,制定的测试运行器需要继承 org.junit.runner.Runner。
断言测试
package cn.z201.learn.java.junit.main;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNotSame;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import cn.z201.learn.java.junit.util.Calculate;
public class ExampleTset {
@Test
public void testSimpleExample() {
System.out.println("this is testSimpleExample");
}
/*
* @BeforeClass 修饰的方法会在所有方法被被调用钱执行,该方法必须是静态的。
* 在测试类加载就会运行,而且在内存中只会存在一个实例。它比较适合加载配置文件。
*/
@BeforeClass
public static void setUpBeforeClass() throws Exception{
System.out.println("this is @BeforeClass");
}
/*
* @AfterClass 所修饰的方法通常用来对资源的清理,修饰方法必须是静态的。
*/
@AfterClass
public static void tearDownAfterClass() throws Exception{
System.out.println("this is @AfterClass");
}
/*
* @Before 修饰的方法会在每个被@Test修饰的方法调用前执行。
*/
@Before
public void setUp(){
System.out.println("this is @setUp");
}
/*
* @After 修饰的方法会在每个被@Test修饰的方法调用后执行。
*/
@After
public void tearDown(){
System.out.println("this is @tearDown");
}
@Test
public void testSimple() {
String str_1 = "this is testSimple";
assertEquals("this is testSimple", str_1); //检查两个变量或者等式是否平衡
int int_1 = 2;
assertFalse(int_1 == new Calculate().add(2, 1)); //检查条件是假的
int int_2 = 3;
assertTrue(int_2 == new Calculate().add(2, 1)); //检查条件是真的
assertNotNull(int_2); //检查对象不是空的
assertNull(null); //检查对象是空的
assertThat(2, is(2)); //判断2 是不是 2
String str3 = null;
String str4 = "abc";
String str5 = "abc";
String[] expectedArray = {"one", "two", "three"};
String[] resultArray = {"one", "two", "three"};
//检查两个对象引用是否指向同一个对象
assertSame(str4,str5);
//检查两个对象引用是否不指向同一个对象
assertNotSame(str3,str4);
//检查两个数组是否相等。
assertArrayEquals(expectedArray, resultArray);
}
}
- 测试类
package cn.z201.learn.java.junit.util;
public class Calculate {
//相加
public int add(int a , int b){
return a + b;
}
//相减
public int subtract(int a , int b){
return a - b;
}
//相乘
public int multipy(int a , int b){
return a * b;
}
//相除
public int divide(int a , int b){
return a / b;
}
}
- 测试用例
package cn.z201.learn.java.junit.main;
import static org.junit.Assert.*;
import org.junit.Test;
import cn.z201.learn.java.junit.util.Calculate;
public class CalculateTest {
@Test(timeout = 10)
public void testAdd(){
assertEquals(6, new Calculate().add(3, 3));
}
@Test
public void testSubtract(){
assertEquals(0, new Calculate().subtract(3, 3));
}
@Test
public void testMultipy(){
assertEquals(9, new Calculate().multipy(3, 3));
}
@Test
public void testDivide(){
assertEquals(1, new Calculate().divide(3, 3));
}
}
参数化测试
package cn.z201.learn.java.junit.main;
import static org.junit.Assert.assertEquals;
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;
import cn.z201.learn.java.junit.util.Calculate;
/**
* 参数化测试
* @author z201
*/
@RunWith(Parameterized.class)
public class ParameterTest {
int expected = 0;
int intput_1 = 0;
int intput_2 = 0;
@Parameters
public static Collection<Object[]> collection(){
return Arrays.asList(new Object[][]{
{3,1,2},{4,2,2}
});
}
public ParameterTest(int expected , int intput_1 ,int intput_2){
this.expected = expected;
this.intput_1 = intput_1;
this.intput_2 = intput_2;
}
/*
* 执行的时候会被调用两次
*/
@Test
public void testAdd(){
assertEquals(expected, new Calculate().add(intput_1, intput_2));
}
}
套件中聚合测试
package cn.z201.learn.java.junit.main;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
/**
* 测试套件
* 测试套件类,必须保证是一个空类,并且是有public 修饰的类
* 测试套件可以执行其他测试套件
*/
@RunWith(Suite.class)
@Suite.SuiteClasses({ExampleTset.class,
CalculateTest.class})
public class SuiteTest {
}