JUnit是什么?
JUnit是一个Java语言的单元测试框架。
使用JUnit的好处
运行测试方法,不论方法内容怎么变,都通过查看接口最终返回的结果,以此来判断接口是否通畅。
首先,导入junit4、hamcrest依赖
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-core</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-library -->
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-library</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
创建一个类T.java,编写好您将要测试的方法
package com.jalor.junit;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @ProjectName: JALOR
* @Package: com.jalor.junit
* @ClassName: T
* @Author: Le Nian
* @Description: junit4单元测试入门
* @Date: 2020/8/9 2:41
* @Version: 1.0
*/
public class T {
/**
* 测试加法
* @param x
* @param y
* @return
*/
public int add(int x, int y){
return x + y;
}
/**
* 测试除法
* @param x
* @param y
* @return
*/
public int divide(int x, int y){
return x / y;
}
/**
* 测试字符串
* @return
*/
public String testString(){
return "hello, world";
}
/**
* 测试HashMap
* @return
*/
public HashMap<String, String> testMap(){
HashMap map = new HashMap();
map.put("key1", "hello");
map.put("key2", "world");
map.put("key3", "helloworld");
return map;
}
/**
* 测试ArrayList集合
* @return
*/
public ArrayList<String> testList(){
ArrayList<String> list = new ArrayList<>();
list.add("hello");
list.add("world");
list.add("helloworld");
return list;
}
}
编写测试类TTest.java
package com.jalor.junit;
import static org.junit.Assert.*;
import static org.hamcrest.Matchers.*;
import org.junit.*;
import java.util.ArrayList;
import java.util.HashMap;
/**
* @ProjectName: JALOR
* @Package: com.jalor.junit
* @ClassName: TTest
* @Author: Le Nian
* @Description: junit4单元测试入门
* @Date: 2020/8/9 2:42
* @Version: 1.0
*/
public class TTest {
/**
* 所有测试开始之前运行
*/
@BeforeClass
public static void beforeClass(){
System.out.println("beforeClass");
}
/**
* 所有测试结束之后运行
*/
@AfterClass
public static void afterClass(){
System.out.println("afterClass");
}
/**
* 每个方法执行之前执行该方法
*/
@Before
public void bfore(){
System.out.println("bfore");
}
/**
* 测试加法
*/
@Test
public void testAdd(){
int z = new T().add(5, 3);
// assertEquals(8, z);
// assertTrue(z > 3);
// assertTrue("z too small", z > 10);
// 1. Failure是指测试失败
// 2. Error是指测试程序本身出错
// int a = 8 / 0;
// 判断z是否等于8
assertThat(z, is(8));
// 判断z是否满足所有条件,即:比1大又比10小
assertThat(z, allOf(greaterThan(1), lessThan(10)));
// 判断z是否满足其中任意条件,即:比10大或比10小
assertThat(z, anyOf(greaterThan(10), lessThan(10)));
// 任意值都通过
assertThat(z, anything());
}
/**
* 测试除法
* expected 期望抛异常,如果抛异常表示测试通过
* timeout 期望该方法在100毫秒内结束
*/
@Test(expected = java.lang.ArithmeticException.class, timeout = 100)
public void testDivide(){
int z = new T().divide(8, 0);
}
/**
* 测试字符串
*/
@Test
public void testString(){
String str = new T().testString();
// 判断字符串是否是hello
// assertThat(str, is("hello"));
// 判断字符串是否不是hello
assertThat(str, not("hello"));
// 判断字符串是否包含hello
assertThat(str, containsString("hello"));
// 判断是否相等
String hello = "hello, world!0";
assertThat(str, equalTo(hello));
}
/**
* 测试HashMap
*/
@Test
public void testMap(){
HashMap<String, String> map = new T().testMap();
// 判断map中是否有key为hello对应的值为hello
// assertThat(map, hasEntry("hello", "hello"));
// 判断map中是否有key为key1对应的值为hello
assertThat(map, hasEntry("key1", "hello"));
// 判断map中是否包含key1的key
assertThat(map, hasKey("key1"));
// 判断map中是否包含hello的value
assertThat(map, hasValue("hell"));
}
/**
* 测试ArrayList集合
* @Ignore表示本次测试(TTest整体测试)中忽略该方法,即:指定该方法不进行测试
*
*/
@Ignore
@Test
public void testList(){
ArrayList<String> list = new T().testList();
// 判断list是否包含hello
assertThat(list, hasItem("hello"));
}
/**
* 每个方法执行之后执行该方法
*/
@After
public void after(){
System.out.println("after");
}
}