My main application class is annotated @EnableR2dbcAuditing, like so:

@SpringBootApplication
@EnableR2dbcAuditing
class Application

I also have a simple router and handler config which I'm trying to write some tests for:

@Configuration
class WebhookRoutes {
    @Bean
    fun webhookRouter(webhookHandler: WebhookHandler) = coRouter {
        "/api/v0/webhook".nest {
            POST("/ie/transactions") { webhookHandler.saveIENotification(it) }
        }
    }
}

@Component
class WebhookHandler {

    suspend fun saveIENotification(serverRequest: ServerRequest): ServerResponse {
        return ServerResponse.ok().buildAndAwait()
    }
}

The actual test (Junit5):

@WebFluxTest
@Import(value = [WebhookHandler::class, WebhookRoutes::class])
internal class WebhookHandlerTest {

    @Autowired
    lateinit var webClient: WebTestClient

    @Test
    fun `receive notifications from Paysolut IE`() {
        webClient.post()
            .uri("/api/v0/webhook/ie/transactions")
            .exchange()
            .expectStatus().is2xxSuccessful
    }
}

When I try to run the test, I get:

Failed to load ApplicationContext java.lang.IllegalStateException: Failed to load ApplicationContext ... Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'r2dbcAuditingHandler': Cannot create inner bean '(inner bean)#706b9118' of type [org.springframework.data.r2dbc.config.PersistentEntitiesFactoryBean] while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#706b9118': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.r2dbc.mapping.R2dbcMappingContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveInnerBean(BeanDefinitionValueResolver.java:389) at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:134) ... Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#706b9118': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.r2dbc.mapping.R2dbcMappingContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ... Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.r2dbc.mapping.R2dbcMappingContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {} at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1790) at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1346) ...

If I move @EnableR2dbcAuditing to another configuration class, the test runs just fine.

I tried both Spring Boot 2.4.1 and 2.4.5

Comment From: wilkinsona

This is the expected behaviour. Moving the @Enable… annotation to another configuration class, as you have described, is the recommended approach.