默认情况下,WebFluxTest 注释不排除依赖于数据源的 Spring beans
I'm setting up a template for a Spring Boot application — on the latest version 3.2.4
. The problem I'm facing is with the test cases.
I'm using @WebFluxTest
to run the test cases on the controllers, but somehow it's not skipping the database-related custom Spring beans :/
This is how the application is configured (skipping imports and constructors for brevity):
@SpringBootApplication(scanBasePackages = { "tld.domain.controller", "tld.domain.service" })
public class Application {
public static void main(final String... args) {
SpringApplication.run(Application.class, args);
}
@EnableTransactionManagement
@Configuration(proxyBeanMethods = false)
@EnableJdbiRepositories(basePackages = "tld.domain.repository")
public static class PersistenceConfig {
@Bean
public Jdbi jdbi(final DataSource dataSource) {
return Jdbi.create(new SpringConnectionFactory(dataSource))
.installPlugin(new SqlObjectPlugin());
}
}
}
The controller, service, and repository classes are quite standard:
@RestController
public class TypeResource {
final TypeService service;
@GetMapping(path = "/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<Type> getByID(@PathVariable("id") final String id) {
return ResponseEntity.of(service.getByID(id));
}
}
@Service
public class TypeService {
final TypeRepository repository;
public Optional<Type> getByID(final String id) {
return repository.getByID(id);
}
}
Finally, the test cases for the sample controller look like:
@ActiveProfiles("test")
@WebFluxTest(controllers = TypeResource.class)
final class TypeResourceTest {
@Autowired
private WebTestClient webClient;
@MockBean
private TypeService service;
...
}
Despite using @WebFluxTest
, the test cases are not skipping the custom Spring beans configuration related to the database, leading to unexpected behavior in the test environment.
These are the relevant portions of the stack trace:
TypeResourceTest > getByID() FAILED
java.lang.IllegalStateException: Failed to load ApplicationContext for [ReactiveWebMergedContextConfiguration@4afd65fd testClass = tld.domain.resource.TypeResourceTest, locations = [], classes = [tld.domain.Application], contextInitializerClasses = [], activeProfiles = ["test"], propertySourceDescriptors = [], propertySourceProperties = ["org.springframework.boot.test.autoconfigure.web.reactive.WebFluxTestContextBootstrapper=true"], contextCustomizers = [org.springframework.boot.testcontainers.service.connection.ServiceConnectionContextCustomizer@0, org.springframework.boot.test.autoconfigure.OverrideAutoConfigurationContextCustomizerFactory$DisableAutoConfigurationContextCustomizer@30f4b1a6, org.springframework.boot.test.autoconfigure.actuate.observability.ObservabilityContextCustomizerFactory$DisableObservabilityContextCustomizer@1f, org.springframework.boot.test.autoconfigure.filter.TypeExcludeFiltersContextCustomizer@14ff57cc, org.springframework.boot.test.autoconfigure.properties.PropertyMappingContextCustomizer@e5029446, org.springframework.boot.test.autoconfigure.web.servlet.WebDriverContextCustomizer@2fb69ff6, [ImportsContextCustomizer@4b5ad306 key = [org.springframework.boot.autoconfigure.security.reactive.ReactiveSecurityAutoConfiguration, org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration, org.springframework.boot.autoconfigure.cache.CacheAutoConfiguration, org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration, org.springframework.boot.autoconfigure.web.reactive.error.ErrorWebFluxAutoConfiguration, org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration, org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration, org.springframework.boot.autoconfigure.security.oauth2.resource.reactive.ReactiveOAuth2ResourceServerAutoConfiguration, org.springframework.boot.test.autoconfigure.web.reactive.WebTestClientAutoConfiguration, org.springframework.boot.autoconfigure.thymeleaf.ThymeleafAutoConfiguration, org.springframework.boot.autoconfigure.gson.GsonAutoConfiguration, org.springframework.boot.autoconfigure.jsonb.JsonbAutoConfiguration, org.springframework.boot.autoconfigure.freemarker.FreeMarkerAutoConfiguration, org.springframework.boot.autoconfigure.web.reactive.WebFluxAutoConfiguration, org.springframework.boot.autoconfigure.security.oauth2.client.reactive.ReactiveOAuth2ClientAutoConfiguration, org.springframework.boot.autoconfigure.http.codec.CodecsAutoConfiguration, org.springframework.boot.autoconfigure.mustache.MustacheAutoConfiguration]], org.springframework.boot.test.context.filter.ExcludeFilterContextCustomizer@d4602a, org.springframework.boot.test.json.DuplicateJsonObjectContextCustomizerFactory$DuplicateJsonObjectContextCustomizer@15515c51, org.springframework.boot.test.mock.mockito.MockitoContextCustomizer@be84b7c9, org.springframework.boot.test.context.SpringBootTestAnnotation@e0ed3db8], contextLoader = org.springframework.boot.test.context.SpringBootContextLoader, parent = null]
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:180)
...
Caused by:
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'jdbi' defined in tld.domain.Application$PersistenceConfig: Unsatisfied dependency expressed through method 'jdbi' parameter 0: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
at app//org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:798)
at app//org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:542)
... 85 more
Caused by:
org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
... 109 more
Any insights or suggestions on how to resolve this issue would be greatly appreciated.
- 本文标签: 家庭宠物
- 本文链接: https://www.coder6.net/article/2438
- 版权声明: 本文由蚂蚁原创发布,转载请遵循《署名-非商业性使用-相同方式共享 4.0 国际 (CC BY-NC-SA 4.0)》许可协议授权