SpringRunner vs. SpringBootTest – SpringRunner vs. SpringBootTest

最后修改: 2023年 10月 7日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Testing is critical for any application, whether a unit test or an integration test. SpringRunner and SpringBootTest classes form the foundation to run integration tests.

无论是单元测试还是集成测试,测试对于任何应用程序都至关重要。SpringRunnerSpringBootTest 类构成了运行集成测试的基础。

In this tutorial, we’ll learn about both. We’ll learn how to use them in our code and learn about their similarities and differences.

在本教程中,我们将了解这两者。我们将学习如何在代码中使用它们,并了解它们的异同。

2. SpringRunner

2. SpringRunner

SpringRunner is an alias for the class SpringJUnit4ClassRunner and works with JUnit4-based test classes. It loads Spring TestContext, through which spring beans and configurations become available to use along with JUnit annotations. We’ll require JUnit 4.12 or higher to use this.

SpringRunnerSpringJUnit4ClassRunner 类的别名,可与 JUnit4 测试类一起使用。它加载 Spring TestContext,通过 Spring TestContext,spring Bean 和配置可与 JUnit 注释一起使用。我们需要 JUnit 4.12 或更高版本才能使用它。

To use this in code, annotate the test class with @RunWith(SpringRunner.class):

要在代码中使用此功能,请在测试类中注释 @RunWith(SpringRunner.class)

@RunWith(SpringRunner.class)
public class SampleIntegrationTest {

    @Test
    public void test() {
        //
    }
}

3. SpringBootTest

3.SpringBootTest</em

SpringBootTest is an alternative to SpringRunner and works with JUnit5. It’s also used to run integration tests and load Spring TestContext.

SpringBootTestSpringRunner 的替代方案,可与 JUnit5 协同工作。它还用于运行集成测试和加载 Spring TestContext

It is very rich and provides many configurations through its annotation arguments. It supports various web environment modes such as MOCK, RANDOM_PORT, DEFINED_PORT, and NONE. We can pass application properties through its annotation that is injected in the spring environment before the test runs:

它的功能非常丰富,可通过注解参数提供多种配置。它支持各种网络环境模式,如 MOCK、RANDOM_PORT、DEFINED_PORTNONE。我们可以在测试运行前通过注入 Spring 环境的注解传递应用程序属性:

@SpringBootTest(
  properties = {"user.name=test_user"},
  webEnvironment = MOCK)
public class SampleIntegrationTest {

    @Test
    public void test() {
        //
    }
}

The annotation @SpringBootTest is required at the class level to run the integration test.

要运行集成测试,需要在类级别使用注解 @SpringBootTest

4. Comparison Between SpringRunner and SpringBootTest

4.SpringRunnerSpringBootTest 之间的比较

In this table, we’ll compare both the classes with their pros and cons.

在本表中,我们将比较这两个类别的优缺点。

SpringRunner SpringBootTest
Use to run integration tests and load Spring TestContext Use to run integration tests and load Spring TestContext
JUnit annotations are also available to use JUnit annotations are also available to use
Needs JUnit4.12 or higher Needs JUnit5 or higher
Not a rich API with respect to configuration Offer a rich API to configure test configuration
Not recommended Recommended as it supports new features and is simple to use

5. Conclusion

5.结论

In this article, we learned about SpringRunner and SpringBootTest. We’ve learned how to use them. We’ve also compared them and learned their differences and similarities.

在本文中,我们了解了SpringRunnerSpringBootTest。我们已经学会了如何使用它们。我们还对它们进行了比较,了解了它们的异同。

We should use SpringBootTest as it supports the latest JUnit, but whenever there is a requirement to use JUnit 4, SpringRunner is the option.

我们应该使用SpringBootTest,因为它支持最新的JUnit、但是,当需要使用 JUnit 4 时, SpringRunner 是一种选择