Properly assert that auto-configured TaskExecutor is lazy

This commit fixes a flawed assertion that was relying on a log message
to validate the TaskExecutor is lazy. The level of the log message has
changed in framework and broke the test. We now rather check the bean
definition.
This commit is contained in:
Stephane Nicoll
2021-05-11 17:17:28 +02:00
parent 2e0481ca28
commit f042dcf0e0

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2020 the original author or authors.
* Copyright 2012-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,13 +23,13 @@ import java.util.function.Consumer;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.task.TaskExecutorBuilder;
import org.springframework.boot.task.TaskExecutorCustomizer;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.ContextConsumer;
import org.springframework.boot.test.system.CapturedOutput;
import org.springframework.boot.test.system.OutputCaptureExtension;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -96,13 +96,13 @@ class TaskExecutionAutoConfigurationTests {
}
@Test
void taskExecutorAutoConfigured(CapturedOutput output) {
void taskExecutorAutoConfiguredIsLazy() {
this.contextRunner.run((context) -> {
assertThat(output).doesNotContain("Initializing ExecutorService");
assertThat(context).hasSingleBean(Executor.class);
assertThat(context).hasBean("applicationTaskExecutor");
assertThat(context).hasSingleBean(Executor.class).hasBean("applicationTaskExecutor");
BeanDefinition beanDefinition = context.getSourceApplicationContext().getBeanFactory()
.getBeanDefinition("applicationTaskExecutor");
assertThat(beanDefinition.isLazyInit()).isTrue();
assertThat(context).getBean("applicationTaskExecutor").isInstanceOf(ThreadPoolTaskExecutor.class);
assertThat(output).contains("Initializing ExecutorService");
});
}