zou*_*ari 45 java junit spring-boot spring-boot-test
在单元测试中,@Runwith(SpringRunner.class)&之间有什么区别@SpringBootTest?
你能向我解释一下每一种的用例吗?
Dea*_*ool 64
@RunWith(SpringRunner.class) :在junit 测试期间@Autowire,您需要此注释来启用spring boot 功能,例如,@MockBean等等
用于在 Spring Boot 测试特性和 JUnit 之间提供桥梁。每当我们在 JUnit 测试中使用任何 Spring Boot 测试功能时,都需要此注释。
@SpringBootTest :此注解用于加载完整的应用程序上下文以进行端到端集成测试
当我们需要引导整个容器时,可以使用 @SpringBootTest 注解。注释通过创建将在我们的测试中使用的 ApplicationContext 来工作。
这是关于这两种情况的清晰示例的文章Baeldung
小智 26
@RunWith 是来自 JUnit 4 的旧注释,用于使用测试运行器。如果您使用的是 JUnit 5 (Jupiter),则应该使用 @ExtendWith 来使用 JUnit 扩展
“如果您使用的是 JUnit 4,请不要忘记将 @RunWith(SpringRunner.class) 添加到您的测试中,否则注释将被忽略。如果您使用的是 JUnit 5,则无需添加等效的 @ExtendWith( SpringExtension.class) 作为@SpringBootTest 和其他@…Test 注释已经用它进行了注释。
| 归档时间: |
|
| 查看次数: |
26098 次 |
| 最近记录: |