是否有任何特殊配置可以将 SpringRunner 与 junit5 一起使用?

Is there any special configuration to use SpringRunner with junit5?

我的微服务项目基于spring-boot框架和我所有的单元测试运行 spring 运行ner.

@RunWith(SpringRunner.class)

添加此注释,导入以下库:

import org.springframework.test.context.junit4.SpringRunner;

如何使用 junit5 将我的测试 类 设置为 运行?

第一个注释@RunWith(SpringRunner.class) 用于在 Spring 引导测试功能和 JUnit 之间提供桥梁。 SpringRunner.class 启用对测试中 bean 的 spring 上下文加载和依赖注入的完全支持。 @SpringBootTest 通过将在我们的测试中使用的 SpringApplication 创建 ApplicationContext 测试。自嵌入式服务器以来,它引导整个容器并创建 Web 环境。

在我们的测试中,我们可以模拟真实的网络环境,将其设置为 RANDOM_PORT,同时加载 WebServerApplicationContext。嵌入式服务器启动并在随机端口上侦听。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = {YourPackage.class}, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class YourClassTest {

    @LocalServerPort
    private int port;

    @Autowired
    TestRestTemplate restTemplate;
    HttpHeaders headers = new HttpHeaders();

    @ParameterizedTest
    @JsonFileSource(resources = "/param.json")
    void createBusinessEntity(JsonObject object){
      ....
    }
}

@LocalServerPort 注释为我们提供了在运行时分配的注入 HTTP 端口。它是 @Value("${local.server.port}").

的方便替代品

为了在 Spring 应用程序中访问第三方 REST 服务,我们使用 Spring RestTemplate 或 TestRestTemplate 方便的替代方法,通过在我们的测试中注入它来适合集成测试 class.在我们的项目中使用 spring-boot-starter-test 依赖项,我们可以在运行时访问 "TestRestTemplate" class.

在我们的测试方法中,我们使用 junit-json-params ,这是一个 Junit 5 库,它提供注释以在参数化测试中从 JSON 字符串或文件加载数据。我们还使用 @ParameterizedTest 注释对该方法进行了注释,以补充下面的库。它用于表示注释方法是参数化测试方法。该方法不能是私有的或静态的。他们还必须通过 @ArgumentsSource 或相应的组合注解指定至少一个 ArgumentsProvider。

我们的@ArgumentsSource 一个JSON 文件@JsonFileSource(resources = "param.json") 我们放在test.resources 包里。 @JsonFileSource 允许您使用 class 路径中的 JSON 文件。它支持单个对象、对象数组和 JSON 原语。

从文件中检索到的 JSON 对象绑定到方法参数 "object" ,它被转换为 POJO 对象,在本例中为我们的实体模型。

在Pom.xml中我们必须导入这些库...

<dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>

        <exclusions>
            <exclusion>
                <groupId>com.vaadin.external.google</groupId>
                <artifactId>android-json</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

       <dependency>
            <groupId>net.joshka</groupId>
            <artifactId>junit-json-params</artifactId>
            <version>5.5.1-r0</version>
        </dependency>

        <dependency>
            <groupId>org.mockito</groupId>
            <artifactId>mockito-junit-jupiter</artifactId>
            <version>${mockito.version}</version>
        </dependency>

        <dependency>
            <groupId>org.junit</groupId>
            <artifactId>junit-bom</artifactId>
            <version>${junit-jupiter.version}</version>
            <scope>import</scope>
            <type>pom</type>
        </dependency>

查看 DZone 和我的博客上 post 的这些文章,您可以在其中访问完整的示例和逐步说明如何使用 Junit 5 测试 spring 启动微服务。 https://dzone.com/articles/microservices-in-publish-subscribe-communication-u https://www.jeevora.com/2019/11/18/publish-subscribe-messaging-systems/

使用 JUnit Jupiter(又名 JUnit 5)不再需要“@RunWith(SpringRunner.class)”,因为这是 JUnit 4 机制。最新版本的 Spring/Spring Boot JUnit 5 支持开箱即用,例如通过使用 ˋspring-boot-starter-testˋ。

我建议在您的 Maven/Gradle 文件中排除对 JUnit 4 的依赖,以减少混淆 JUnit 4 和 5 功能的可能性。

这是一篇介绍基础知识的文章:https://howtodoinjava.com/spring-boot2/testing/junit5-with-spring-boot2/

从构建路径中删除 JUnit4。

例如:

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
@TestPropertySource(locations = "classpath:application-local.properties")
public class MyTest {
    @Before
    public void setUp() {
        ...
    }

    @Test
    public void testMethod() {
        Assert.assertTrue(...);
    }
}

会变成

@SpringBootTest(classes = Application.class)
@TestPropertySource(locations = "classpath:application-local.properties")
public class MyTest {
    @BeforeEach
    public void setUp() {
        ...
    }
    @Test
    public void testMethod() {
       Assertions.assertTrue(...);
    }
}

Spring 2.4 似乎包含 JUnit 5 并使其成为开箱即用的默认设置。

除了将 @RunWith(SpringJUnit4ClassRunner.class) 更新为 @ExtendWith(SpringExtension.class) 之外,我还必须将以下内容添加到 build.gradle 以便测试实际上 运行:

test {
    useJUnitPlatform {}
}

这最后一步可能是由于 JUnit 4 是我的一个依赖项的依赖项,但我阅读的所有其他内容都没有表明这是必需的。