【问题标题】:Is there any special configuration to use SpringRunner with junit5? [duplicate]是否有任何特殊配置可以将 SpringRunner 与 junit5 一起使用?
【发布时间】:2020-07-20 03:44:46
【问题描述】:

我的基于spring-boot框架的微服务项目和我所有的单元测试都是用spring runner运行的。

@RunWith(SpringRunner.class)

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

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

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

【问题讨论】:

    标签: spring-boot junit junit5 spring-boot-test springrunner


    【解决方案1】:

    使用 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/

    【讨论】:

      【解决方案2】:

      从构建路径中删除 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(...);
          }
      }
      

      【讨论】:

        【解决方案3】:

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

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

        test {
            useJUnitPlatform {}
        }
        

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

        【讨论】:

          【解决方案4】:

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

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

          @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 方便的替代方案,通过将其注入到我们的测试类中来进行集成测试。在我们的项目中使用 spring-boot-starter-test 依赖,我们可以在运行时访问“TestRestTemplate”类。

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

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

          从文件中检索到的 JSON 对象绑定到方法 params “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 和我的博客上的这些文章,您可以在其中访问完整的示例和逐步说明如何使用 Junit 5 测试 Spring Boot 微服务。 https://dzone.com/articles/microservices-in-publish-subscribe-communication-u https://www.jeevora.com/2019/11/18/publish-subscribe-messaging-systems/

          【讨论】:

          • 感谢详细解答
          猜你喜欢
          • 1970-01-01
          • 2020-05-09
          • 2012-09-04
          • 2016-07-24
          • 2015-05-08
          • 2012-07-14
          • 1970-01-01
          • 2021-11-12
          • 2016-04-01
          相关资源
          最近更新 更多