SpringRunner 与 SpringBootTest
SpringRunner vs SpringBootTest
在单元测试中,@Runwith(SpringRunner.class)
和@SpringBootTest
有什么区别?
你能给我解释一下每一个的用例吗?
来自 spring.io :
@RunWith(SpringRunner.class)
tells JUnit to run using Spring’s testing
support. SpringRunner
is the new name for SpringJUnit4ClassRunner
,
it’s just a bit easier on the eye.
@SpringBootTest
is saying “bootstrap with Spring Boot’s support” (e.g.
load application.properties
and give me all the Spring Boot goodness)
因此,如果您不需要 Spring Boot 为您的集成测试加载的所有内容,您可能不需要 @SpringBootTest
@RunWith(SpringRunner.class) : 你需要这个注释来启用 spring 引导功能,比如 @Autowire
, @MockBean
等等 junit 测试期间
is used to provide a bridge between Spring Boot test features and JUnit. Whenever we are using any Spring Boot testing features in our JUnit tests, this annotation will be required.
@SpringBootTest : 该注解用于加载完整的应用程序上下文以进行端到端集成测试
The @SpringBootTest annotation can be used when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests.
这篇文章在这两种情况下都有清晰的例子Baeldung
@RunWith 是 JUnit 4 中使用测试运行程序的旧注解。如果您使用的是 JUnit 5 (Jupiter),您应该使用 @ExtendWith 来使用 JUnit 扩展
"如果您使用的是 JUnit 4,请不要忘记也将 @RunWith(SpringRunner.class) 添加到您的测试中,否则注释将被忽略。如果您使用的是 JUnit 5,则不需要添加等效的 @ExtendWith(SpringExtension.class) 作为 @SpringBootTest 和其他 @...Test 注释已经用它注释了。
在单元测试中,@Runwith(SpringRunner.class)
和@SpringBootTest
有什么区别?
你能给我解释一下每一个的用例吗?
来自 spring.io :
@RunWith(SpringRunner.class)
tells JUnit to run using Spring’s testing support.SpringRunner
is the new name forSpringJUnit4ClassRunner
, it’s just a bit easier on the eye.
@SpringBootTest
is saying “bootstrap with Spring Boot’s support” (e.g. loadapplication.properties
and give me all the Spring Boot goodness)
因此,如果您不需要 Spring Boot 为您的集成测试加载的所有内容,您可能不需要 @SpringBootTest
@RunWith(SpringRunner.class) : 你需要这个注释来启用 spring 引导功能,比如 @Autowire
, @MockBean
等等 junit 测试期间
is used to provide a bridge between Spring Boot test features and JUnit. Whenever we are using any Spring Boot testing features in our JUnit tests, this annotation will be required.
@SpringBootTest : 该注解用于加载完整的应用程序上下文以进行端到端集成测试
The @SpringBootTest annotation can be used when we need to bootstrap the entire container. The annotation works by creating the ApplicationContext that will be utilized in our tests.
这篇文章在这两种情况下都有清晰的例子Baeldung
@RunWith 是 JUnit 4 中使用测试运行程序的旧注解。如果您使用的是 JUnit 5 (Jupiter),您应该使用 @ExtendWith 来使用 JUnit 扩展
"如果您使用的是 JUnit 4,请不要忘记也将 @RunWith(SpringRunner.class) 添加到您的测试中,否则注释将被忽略。如果您使用的是 JUnit 5,则不需要添加等效的 @ExtendWith(SpringExtension.class) 作为 @SpringBootTest 和其他 @...Test 注释已经用它注释了。