Spring 启动 - 测试 - 拆解一个 bean
Spring Boot - Testing - tearDown for a bean
我使用 @EmbeddedKafka 注释如下来获得一个 kafka 模拟:
@ExtendWith(SpringExtension.class)
@SpringBootTest
@EmbeddedKafka(partitions = 1,
topics = {"topic"},
brokerProperties = {
"auto.create.topics.enable=${topics.autoCreate:false}",
"delete.topic.enable=${topic.delete:true}",
"broker.id=2"})
public class KafkaUsersTest {
@Autowired
private EmbeddedKafkaBroker embeddedKafka;
@Test
public void test1() {
// test something
}
@Test
public void test2() {
// test something
}
...
}
现在,在测试完成后,我想关闭嵌入式 Kafka bean。像这样:
@AfterAll
public void tearDown(){
embeddedKafka.getKafkaServers().forEach(KafkaServer::shutdown);
embeddedKafka.getKafkaServers().forEach(KafkaServer::awaitShutdown);
}
问题是:
- @AfterAll 方法只能是静态的。
- 如果我将其设为静态 - 那么 embeddedKafka 也必须是静态的,@Autowired 注释将不起作用。
我想我可以从其中一个测试中将 bean 转换为静态字段,然后在 tearDown() 中使用它,但这真的很难看。
什么是 "good practice" 在所有测试完成后仅关闭一次 bean?
An @AfterAll method can only be static.
这不是真的。
Denotes that the annotated method should be executed after all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class; analogous to JUnit 4’s @AfterClass. Such methods are inherited (unless they are hidden or overridden) and must be static (unless the "per-class" test instance lifecycle is used).
如果使用 @TestInstance(Lifecycle.PER_CLASS)
,@AfterAll
方法可以是非静态方法。这也记录在 JUnit 5 User Guide:
The "per-class" mode has some additional benefits over the default "per-method" mode. Specifically, with the "per-class" mode it becomes possible to declare @BeforeAll and @AfterAll on non-static methods as well as on interface default methods.
我使用 @EmbeddedKafka 注释如下来获得一个 kafka 模拟:
@ExtendWith(SpringExtension.class)
@SpringBootTest
@EmbeddedKafka(partitions = 1,
topics = {"topic"},
brokerProperties = {
"auto.create.topics.enable=${topics.autoCreate:false}",
"delete.topic.enable=${topic.delete:true}",
"broker.id=2"})
public class KafkaUsersTest {
@Autowired
private EmbeddedKafkaBroker embeddedKafka;
@Test
public void test1() {
// test something
}
@Test
public void test2() {
// test something
}
...
}
现在,在测试完成后,我想关闭嵌入式 Kafka bean。像这样:
@AfterAll
public void tearDown(){
embeddedKafka.getKafkaServers().forEach(KafkaServer::shutdown);
embeddedKafka.getKafkaServers().forEach(KafkaServer::awaitShutdown);
}
问题是:
- @AfterAll 方法只能是静态的。
- 如果我将其设为静态 - 那么 embeddedKafka 也必须是静态的,@Autowired 注释将不起作用。
我想我可以从其中一个测试中将 bean 转换为静态字段,然后在 tearDown() 中使用它,但这真的很难看。
什么是 "good practice" 在所有测试完成后仅关闭一次 bean?
An @AfterAll method can only be static.
这不是真的。
Denotes that the annotated method should be executed after all @Test, @RepeatedTest, @ParameterizedTest, and @TestFactory methods in the current class; analogous to JUnit 4’s @AfterClass. Such methods are inherited (unless they are hidden or overridden) and must be static (unless the "per-class" test instance lifecycle is used).
如果使用 @TestInstance(Lifecycle.PER_CLASS)
,@AfterAll
方法可以是非静态方法。这也记录在 JUnit 5 User Guide:
The "per-class" mode has some additional benefits over the default "per-method" mode. Specifically, with the "per-class" mode it becomes possible to declare @BeforeAll and @AfterAll on non-static methods as well as on interface default methods.