sat*_*the 14 java spring spring-boot
我对 SpringRunner 类感到困惑。\n来自 Stackoverflow 和 google 确实理解了这一点SpringRunner并且SpringJunit4ClassRunner它们是一回事。
@RunWith(SpringRunner.class)\nRun Code Online (Sandbox Code Playgroud)\n\n我对这门课的理解是:
\n\n@RunWith有了 Junit5 和 spring boot,这个类不再需要了吗?
\n\n如果是的话,我们应该在 Spring Boot 和 Junit5 环境中使用什么?
\n有了 Junit5 和 spring boot,这个类就不再需要了吗?
该注释@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>
Run Code Online (Sandbox Code Playgroud)
@RunWith是一个 JUnit4 API。
在 JUnit 5 中,请@ExtendedWith改为使用。Spring core为其提供了一个新的SpringExtension。还有一些更灵活的注释可用,例如。SpringJunitConfig
@SpringJunitConfig(classes= config classes)
public class ASpringTest
{}
Run Code Online (Sandbox Code Playgroud)
关于 SpringJunitConfig 的使用,请查看这里的更多示例。
在最新的 Spring Boot 2.4 中,JUnit 5 是默认的测试运行器,使用@SpringBootTest就足够了,而且 Spring Boot 还为 Spring Data 提供了测试切片功能,请查看我的示例。
| 归档时间: |
|
| 查看次数: |
10275 次 |
| 最近记录: |