定义
这是百度百科对Junit的定义:
实操
1. 编写被测试类
package com.example.demo.junit;
/**
* Junit演示
*
* @ClassName JunitDemo
* @Author zhaoguang
* @Date 2019/12/13
* @Version 1.0
*/
public class JunitDemo {
/**
* 测试方法
*
* @param a
* @param b
* @return
*/
public int add(int a, int b) {
return a + b;
}
}
2.单元测试
package com.example.demo.junit;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
/**
* @ClassName JunitDemoTest
* @Author zhaoguang
* @Date 2019/12/13
* @Version 1.0
*/
public class JunitDemoTest {
private JunitDemo demo = null;
@BeforeEach //junit5之后需要用@BeforeEach,5之前是@Before
public void before() {
demo = new JunitDemo();
}
@Test
public void testAdd() {
Assert.assertEquals(3, demo.add(1, 2));
}
@AfterEach
public void AfterEach() {
demo = null;
}
}