有什么特殊配置可以将SpringRunner与junit5一起使用吗



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

@RunWith(SpringRunner.class)

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

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

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

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

使用JUnit Jupiter(又称JUnit 5(不再需要"@RunWith(SpringRunner.class(",因为这是JUnit 4机制。随着最近版本的Spring/Spring Boot JUnit 5的支持开箱即用,例如通过使用?Spring Boot starter test。

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

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

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

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

test {
useJUnitPlatform {}
}

最后一步可能是由于JUnit4是我的一个依赖项的依赖项,但我读到的其他所有东西都表明这是不必要的。

第一个注释@RunWith(SpringRunner.class(用于提供Spring Boot测试特性和JUnit之间的桥梁。SpringRunner.class完全支持测试中bean的spring上下文加载和依赖项注入@SpringBootTest通过SpringApplication创建ApplicationContext测试,这些测试将在我们的测试中使用。它从嵌入式服务器开始引导整个容器,并创建一个web环境。

在我们的测试中,我们可以模拟真实的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,这是一种方便的替代方案,通过在我们的测试类中注入它,适合集成测试。在我们的项目中有了spring-boot-starter测试依赖项,我们可以在运行时访问"TestRestTemplate"类。

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

我们的@ArgumentsSource是一个JSON文件@JsonFileSource(resources="param.JSON"(,我们将其放入test.resources包中@JsonFileSource允许您使用类路径中的JSON文件。它支持单个对象、对象数组和JSON基元。

从文件中检索到的JSON对象绑定到方法params"对象",该对象被转换为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-uhttps://www.jeevora.com/2019/11/18/publish-subscribe-messaging-systems/

最新更新