我对SpringRunner类感到困惑。从Stackoverflow和google了解到SpringRunner
和SpringJunit4ClassRunner
是一样的。
@RunWith(SpringRunner.class)
我对这门课的理解是:
@RunWith
执行单元测试时使用。使用Junit5和spring引导,这个类不再需要了吗?
如果是,那么我们应该在spring引导和Junit5环境中使用什么?
发布于 2020-02-24 05:26:00
来自参考文档
如果您正在使用JUnit 4,请不要忘记在测试中添加@RunWith(SpringRunner.class),否则注释将被忽略。如果您使用的是JUnit 5,则不需要将等效的@ExtendWith(SpringExtension.class)添加为@SpringBootTest和其他@…已经用它对测试注释进行了注释。
发布于 2020-02-24 09:33:37
使用Junit5和spring引导,这个类不再需要了吗?
注释@RunWith(SpringRunner.class)
不再是必需的,可以删除它。
此外,如果只使用Junit5,则建议排除Junit4:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
发布于 2021-01-25 09:46:08
@RunWith
是一个JUnit4 API。
在JUnit 5中,使用@ExtendedWith
代替。Spring核心为它提供了一个新的SpringExtension。还有一些更灵活的注释,例如。SpringJunitConfig
@SpringJunitConfig(classes= config classes)
public class ASpringTest
{}
有关SpringJunitConfig的用法,请查看这里中的更多示例。
在最新的SpringBoot2.4中,JUnit 5是默认的测试运行程序,使用@SpringBootTest
就足够了,Spring还为Spring提供了测试切片功能,请查看我的示例这里。
https://stackoverflow.com/questions/60369022
复制相似问题