我的基于 spring-boot 框架的微服务项目和我所有的单元测试都使用 spring runner 运行。
@RunWith(SpringRunner.class)
添加此注释,导入以下库:
import org.springframework.test.context.junit4.SpringRunner;
如何设置我的测试类以使用 junit5 运行?
我的基于 spring-boot 框架的微服务项目和我所有的单元测试都使用 spring runner 运行。
@RunWith(SpringRunner.class)
添加此注释,导入以下库:
import org.springframework.test.context.junit4.SpringRunner;
如何设置我的测试类以使用 junit5 运行?
使用 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 是我的一个依赖项的依赖项,但我读到的所有其他内容都没有表明这是必要的。
第一个注解@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-发布-订阅-通信-u https://www.jeevora.com/2019/11/18/publish-subscribe-messaging-systems/