java单元测试:JUnit测试运行器

JUnit测试运行器(Test Runner)决定了JUnit如何执行测试。JUnit有多个测试运行器,每个运行器都有特定的功能和用途。

1. 默认运行器

当没有显式指定运行器时,JUnit会使用默认运行器,这在JUnit 4和JUnit 5之间有所不同。

JUnit 4 默认运行器

在JUnit 4中,默认运行器是BlockJUnit4ClassRunner

java 复制代码
import org.junit.Test;
import static org.junit.Assert.assertEquals;

public class DefaultRunnerTest {

    @Test
    public void testAdd() {
        assertEquals(5, 2 + 3);
    }
}
JUnit 5 默认运行器

在JUnit 5中,不需要显式指定运行器,JUnit Jupiter会自动运行测试。

java 复制代码
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class DefaultRunnerTest {

    @Test
    public void testAdd() {
        assertEquals(5, 2 + 3);
    }
}

2. @RunWith 注解

@RunWith注解用于指定JUnit 4的测试运行器。以下是一些常用的运行器:

2.1 SpringRunner

SpringRunner(原名SpringJUnit4ClassRunner)用于在JUnit 4中运行Spring测试,提供Spring应用程序上下文的支持。

java 复制代码
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringRunner;
import static org.junit.Assert.assertEquals;

@RunWith(SpringRunner.class)
public class SpringRunnerTest {

    @Test
    public void testAdd() {
        assertEquals(5, 2 + 3);
    }
}
2.2 Parameterized

Parameterized运行器用于运行参数化测试,即同一个测试方法可以用不同的参数多次运行。

java 复制代码
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import java.util.Arrays;
import java.util.Collection;
import static org.junit.Assert.assertEquals;

@RunWith(Parameterized.class)
public class ParameterizedTest {

    private int input1;
    private int input2;
    private int expected;

    public ParameterizedTest(int input1, int input2, int expected) {
        this.input1 = input1;
        this.input2 = input2;
        this.expected = expected;
    }

    @Parameterized.Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][] {
            { 1, 2, 3 },
            { 2, 3, 5 },
            { 3, 5, 8 }
        });
    }

    @Test
    public void testAdd() {
        assertEquals(expected, input1 + input2);
    }
}
2.3 Suite

Suite运行器用于运行一组测试类。

java 复制代码
import org.junit.runner.RunWith;
import org.junit.runners.Suite;

@RunWith(Suite.class)
@Suite.SuiteClasses({
    TestClass1.class,
    TestClass2.class
})
public class TestSuite {
    // 空类,仅用于运行指定的测试类
}

3. JUnit 5 扩展模型

在JUnit 5中,不使用@RunWith,而是使用@ExtendWith注解来扩展测试功能。

3.1 SpringExtension

SpringExtension用于在JUnit 5中运行Spring测试,提供Spring应用程序上下文的支持。

java 复制代码
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(SpringExtension.class)
public class SpringExtensionTest {

    @Test
    public void testAdd() {
        assertEquals(5, 2 + 3);
    }
}
3.2 ParameterizedTest

@ParameterizedTest注解用于运行参数化测试,即同一个测试方法可以用不同的参数多次运行。

java 复制代码
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ParameterizedTestExample {

    @ParameterizedTest
    @CsvSource({
        "1, 2, 3",
        "2, 3, 5",
        "3, 5, 8"
    })
    void testAdd(int input1, int input2, int expected) {
        assertEquals(expected, input1 + input2);
    }
}

4. 自定义运行器和扩展

4.1 自定义JUnit 4 运行器

可以创建自定义运行器来扩展JUnit 4的功能。

java 复制代码
import org.junit.runners.BlockJUnit4ClassRunner;
import org.junit.runners.model.InitializationError;

public class CustomRunner extends BlockJUnit4ClassRunner {

    public CustomRunner(Class<?> klass) throws InitializationError {
        super(klass);
    }

    @Override
    protected void runChild(org.junit.runners.model.FrameworkMethod method, org.junit.runner.notification.RunNotifier notifier) {
        System.out.println("Running test: " + method.getName());
        super.runChild(method, notifier);
    }
}

import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(CustomRunner.class)
public class CustomRunnerTest {

    @Test
    public void testAdd() {
        assertEquals(5, 2 + 3);
    }
}
4.2 自定义JUnit 5 扩展

可以创建自定义扩展来扩展JUnit 5的功能。

java 复制代码
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public class CustomExtension implements BeforeEachCallback {

    @Override
    public void beforeEach(ExtensionContext context) {
        System.out.println("Before each test: " + context.getDisplayName());
    }
}

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;

@ExtendWith(CustomExtension.class)
public class CustomExtensionTest {

    @Test
    public void testAdd() {
        assertEquals(5, 2 + 3);
    }
}

5. 使用示例:Spring Boot 测试

结合使用@ExtendWith@SpringBootTest进行Spring Boot应用程序的集成测试。

java 复制代码
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;

@ExtendWith(SpringExtension.class)
@SpringBootTest
public class MyApplicationTests {

    @Autowired
    private MyService myService;

    @Test
    public void testAdd() {
        assertEquals(5, myService.add(2, 3));
    }
}

总结

  • JUnit 4 中,@RunWith用于指定测试运行器,常用的运行器包括SpringRunnerParameterizedSuite等。
  • JUnit 5 中,@ExtendWith用于扩展测试功能,常用的扩展包括SpringExtensionParameterizedTest等。
  • 可以创建自定义运行器(JUnit 4)或扩展(JUnit 5)来满足特定测试需求。
  • @SpringBootTest用于Spring Boot应用程序的集成测试。

这些工具和技术使得JUnit能够灵活地适应各种测试需求。

相关推荐
ok!ko3 小时前
设计模式之原型模式(通俗易懂--代码辅助理解【Java版】)
java·设计模式·原型模式
2402_857589364 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
吾爱星辰4 小时前
Kotlin 处理字符串和正则表达式(二十一)
java·开发语言·jvm·正则表达式·kotlin
哎呦没5 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
编程、小哥哥5 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
搁浅°8795 小时前
spring6启用Log4j2日志
单元测试·log4j
IT学长编程6 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统
莹雨潇潇6 小时前
Docker 快速入门(Ubuntu版)
java·前端·docker·容器
杨哥带你写代码7 小时前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端
郭二哈7 小时前
C++——模板进阶、继承
java·服务器·c++