gpt4 book ai didi

java - @SpringBootTest + @BeforeAll

转载 作者:行者123 更新时间:2023-12-01 10:03:21 29 4
gpt4 key购买 nike

我有一个带有数据库和 rabbitmq 用法的小型 Spring Boot 应用程序。
所以我想用集成测试(H2 + apache qpid)进行测试。

@ExtendWith(SpringExtension.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = TestSpringConfig.class)
@TestInstance(TestInstance.Lifecycle.PER_CLASS)

正如我的应用程序所期望的那样,数据库和 mq 我使用 @BeforeAll 来启动它:
@BeforeAll
public void before() {
startMessageBroker();
startDatabase();
}

问题是我的 Web 应用程序在 @BeforeAll 中定义的 database/mq 之前启动。

org.springframework.test.context.junit.jupiter.SpringExtension:
public class SpringExtension implements BeforeAllCallback, AfterAllCallback, TestInstancePostProcessor,
BeforeEachCallback, AfterEachCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback,
ParameterResolver {
// ...
@Override
public void beforeAll(ExtensionContext context) throws Exception {
getTestContextManager(context).beforeTestClass();
}
// ...
@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) throws Exception {
getTestContextManager(context).prepareTestInstance(testInstance);
}
// ...

Web 应用程序在 postProcessTestInstance 阶段和 beforeAll 中的 @BeforeAll 方法开始。

org.junit.platform.engine.support.hierarchical.HierarchicalTestExecutor:
private void execute(TestDescriptor testDescriptor, C parentContext, ExecutionTracker tracker) {
Node<C> node = asNode(testDescriptor);
tracker.markExecuted(testDescriptor);

C preparedContext;
try {
preparedContext = node.prepare(parentContext); // 1 <<<
SkipResult skipResult = node.shouldBeSkipped(preparedContext);
if (skipResult.isSkipped()) {
this.listener.executionSkipped(testDescriptor, skipResult.getReason().orElse("<unknown>"));
return;
}
}
catch (Throwable throwable) {
rethrowIfBlacklisted(throwable);
// We call executionStarted first to comply with the contract of EngineExecutionListener
this.listener.executionStarted(testDescriptor);
this.listener.executionFinished(testDescriptor, TestExecutionResult.failed(throwable));
return;
}

this.listener.executionStarted(testDescriptor);

TestExecutionResult result = singleTestExecutor.executeSafely(() -> {
C context = preparedContext;
try {
context = node.before(context); // 2 <<<

C contextForDynamicChildren = context;
context = node.execute(context, dynamicTestDescriptor -> {
this.listener.dynamicTestRegistered(dynamicTestDescriptor);
execute(dynamicTestDescriptor, contextForDynamicChildren, tracker);
});

C contextForStaticChildren = context;
// @formatter:off
testDescriptor.getChildren().stream()
.filter(child -> !tracker.wasAlreadyExecuted(child))
.forEach(child -> execute(child, contextForStaticChildren, tracker));
// @formatter:on
}
finally {
node.after(context);
}
});

this.listener.executionFinished(testDescriptor, result);
}

参见第 1 点和第 2 点。先执行“prepare”,然后执行“before”。

我不确定是 junit、SpringExtension 的问题还是我做错了什么。
有什么建议吗?

junit-jupiter: 5.0.1

spring-test: 5.0.0.RELEASE

spring-boot-test: 1.5.8.RELEASE

最佳答案

结帐 https://www.testcontainers.org/作为 JUnit 测试的一部分,它提供了与 JUnit 的集成以在 docker 容器中启动 RabbitMQ 和数据库。这使得集成测试更加现实,因为您使用的数据库和消息队列版本将在生产中使用。

关于java - @SpringBootTest + @BeforeAll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46979604/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com