官网:https://junit.org/
JUnit是一个Java语言的单元测试框架,Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何完成功能和完成什么样的功能,Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<scope>testscope>
dependency>
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 multiply(int a , int b){
return a * b;
}
public int divide(int a , int b){
return a / b;
}
}
在test目录下与要测试的类相同包路径下创建测试类,也可以选择要测试的类的类名点击右键—Go To—Test进行快捷创建
这里的junit版本选择junit4
public class CalculateTest {
@BeforeClass
public static void beforeClass(){
System.out.println("beforeClass…………");
}
@Before
public void before(){
System.out.println("before^^^^^^^^");
}
@Test
public void testAdd() {
Calculate calculate = new Calculate();
int add = calculate.add(1, 2);
System.out.println(add);
}
@Test
public void testSubtract() {
Calculate calculate = new Calculate();
int subtract = calculate.subtract(3, 1);
System.out.println(subtract);
}
@Test
public void testMultiply() {
Calculate calculate = new Calculate();
int multiply = calculate.multiply(1, 3);
System.out.println(multiply);
}
@Test
public void testDivide() {
Calculate calculate = new Calculate();
int divide = calculate.divide(10, 2);
System.out.println(divide);
}
@AfterClass
public static void afterClass(){
System.out.println("afterClass…………");
}
@After
public void after(){
System.out.println("after^^^^^^^");
}
}
<parent>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-parentartifactId>
<version>2.4.3version>
parent>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starterartifactId>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<scope>testscope>
dependency>
@SpringBootApplication
public class SpringBootApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootApplication.class,args);
}
}
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 multiply(int a , int b){
return a * b;
}
public int divide(int a , int b){
return a / b;
}
}
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest()
public class SpringBootJunit4 {
@Test
public void add(){
Calculate calculate = new Calculate();
int add = calculate.add(1, 2);
System.out.println(add);
}
}
<dependency>
<groupId>org.junit.jupitergroupId>
<artifactId>junit-jupiterartifactId>
<version>5.7.1version>
<scope>testscope>
dependency>
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-testartifactId>
<scope>testscope>
dependency>
类似于断言,只不过不满足的断言会使得测试方法失败,而不满足的前置条件只会使得测试方法的执行终止,可以看成是测试方法执行的前提,当该前提不满足时,就没有继续执行的必要
import org.junit.jupiter.api.*;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.concurrent.TimeUnit;
import static org.junit.jupiter.api.Assertions.*;
@SpringBootTest
public class SpringBootJunit5 {
@BeforeAll
public static void before(){
System.out.println("before^^^^^^^");
}
@RepeatedTest(2)
@DisplayName("测试一下")
@Test
public void add(){
Calculate calculate = new Calculate();
int add = calculate.add(1, 2);
System.out.println(add);
}
@Test
@Timeout(value = 500 , unit = TimeUnit.MILLISECONDS)
public void divide(){
Calculate calculate = new Calculate();
int divide = calculate.divide(8, 2);
System.out.println(divide);
try {
Thread.sleep(501);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
/**
* 如果预期值与实际值一致,正常执行
* 如果预期值与实际值不一样,打印错误信息和预期值、实际值
*/
@Test
public void multiply(){
Calculate calculate = new Calculate();
int multiply = calculate.multiply(1, 2);
System.out.println(multiply);
assertEquals(multiply ,2 , "两个对象一样");
}
@Test
public void subtract(){
Assumptions.assumeTrue(false,"不满足就不执行");
Calculate calculate = new Calculate();
int subtract = calculate.subtract(5, 1);
System.out.println(subtract);
}
@AfterAll
public static void after(){
System.out.println("after^^^^^^^");
}
}
注解@DisplayName和@RepeatedTest执行效果,使用@RepeatedTest注解会另加执行指定次数所标注的方法
注解@Timeout执行效果,超过指定时间会失败
断言不一致的执行效果,程序会失败,并指出预期值与实际值
前置条件不一致的执行效果,方法不执行,打印指定的消息
import org.junit.jupiter.api.*;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
@DisplayName("嵌套测试")
public class TestNested {
@BeforeAll
public static void before(){
System.out.println("beforeAll^^^^^^");
}
@Test
@DisplayName("外层测试方法1")
void outer1() {
System.out.println("在嵌套测试情况下,外层的test不能驱动内层的beforeAll等方法,内层的test可以驱动外层");
}
@Test
@DisplayName("外层测试方法2")
String outer2(){
return "外层测试方法2";
}
@Nested
@DisplayName("内部类1")
class InnerClass1{
@BeforeEach
void innerClass1BeforeEach() {
System.out.println("InnerClass1BeforeEach1^^^^^^^");
}
@Test
@DisplayName("内部类1内部方法1")
void InnerClass1Method1() {
String s = outer2();
System.out.println("内部类1内部方法1 " +s);
}
@Nested
@DisplayName("内部类2")
class InnerClass2 {
@BeforeEach
void innerClass2BeforeEach() {
System.out.println("InnerClass2BeforeEach1^^^^^^^^^^");
}
@Test
@DisplayName("内部类2内部方法2")
void InnerClass2Method2() {
System.out.println("内部类2内部方法2");
}
}
}
}
可以指定测试方法的输入参数
package com.haomu;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.junit.jupiter.params.provider.ValueSource;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.stream.Stream;
@SpringBootTest
public class ParametricTest {
@ParameterizedTest
@DisplayName("参数化测试1")
@ValueSource(ints = {1, 2, 3, 4, 5})
void testParameterized(int i) {
System.out.println(i);
}
static Stream<String> stringProvider() {
return Stream.of("apple", "banana");
}
@ParameterizedTest
@DisplayName("参数化测试2")
@MethodSource("stringProvider")
void testParameterized2(String s) {
System.out.println(s);
}
}
这些就是关于junit的介绍,如有问题请指出,谢谢阅读!