micronautmongo-java-drivermicronaut-datamicronaut-test

Why Micronaut Test closed the MongoClient connection


I'm about testing using MongoDB. Instead of Micronaut Test Resources I'm using local hosted docker container for database (I prefer to see the result). Many my tests depends on MongoDB but some throws that error:

17:34:09.134 [default-nioEventLoopGroup-3-4] INFO  org.mongodb.driver.connection - Opened connection [connectionId{localValue:6, serverValue:26}] to localhost:27018
17:34:09.329 [default-nioEventLoopGroup-3-10] ERROR InternalError - 
java.lang.IllegalStateException: state should be: open
    at com.mongodb.assertions.Assertions.isTrue(Assertions.java:79)
    at com.mongodb.internal.connection.BaseCluster.getDescription(BaseCluster.java:166)
    at com.mongodb.internal.connection.SingleServerCluster.getDescription(SingleServerCluster.java:44)
    at com.mongodb.client.internal.MongoClientDelegate.getConnectedClusterDescription(MongoClientDelegate.java:144)
    at com.mongodb.client.internal.MongoClientDelegate.createClientSession(MongoClientDelegate.java:101)
    at com.mongodb.client.internal.MongoClientImpl.startSession(MongoClientImpl.java:143)
    at com.mongodb.client.internal.MongoClientImpl.startSession(MongoClientImpl.java:132)
    at io.micronaut.data.mongodb.operations.DefaultMongoRepositoryOperations.withClientSession(DefaultMongoRepositoryOperations.java:753)
    at io.micronaut.data.mongodb.operations.DefaultMongoRepositoryOperations.findAll(DefaultMongoRepositoryOperations.java:253)
    at io.micronaut.data.runtime.intercept.DefaultFindAllInterceptor.intercept(DefaultFindAllInterceptor.java:51)
    at io.micronaut.data.runtime.intercept.DefaultFindAllInterceptor.intercept(DefaultFindAllInterceptor.java:35)
    at io.micronaut.data.intercept.DataIntroductionAdvice.intercept(DataIntroductionAdvice.java:81)
    at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:137)
    at io.micronaut.validation.ValidatingInterceptor.intercept(ValidatingInterceptor.java:143)
    at io.micronaut.aop.chain.MethodInterceptorChain.proceed(MethodInterceptorChain.java:137)

What should I do that state will be open through all my tests?


Solution

  • Solved this via replace DefaultMongoClientFactory. Difference between mine factory and Micronaut edition in the preDestroy action

    at DefaultMongoClientFactory: 45

    @Bean(preDestroy = "close")
    

    Workaround for tests:

    package org.example.util.mongo
    
    import com.mongodb.MongoClientSettings
    import com.mongodb.client.MongoClient
    import com.mongodb.client.MongoClients
    import io.micronaut.configuration.mongo.core.DefaultMongoConfiguration
    import io.micronaut.configuration.mongo.sync.DefaultMongoClientFactory
    import io.micronaut.context.annotation.*
    import jakarta.inject.Singleton
    
    
    @Factory
    @Requires(
        classes = [MongoClient::class],
        beans = [DefaultMongoConfiguration::class]
    )
    @Replaces(DefaultMongoClientFactory::class)
    class MongoClientFactoryForTest {
    
        @Primary
        @Singleton
        @Replaces(MongoClient::class)
        fun mongoClient(settings: MongoClientSettings): MongoClient {
    
            return MongoClients.create(settings)
        }
    }
    
    @MicronautTest
    @Requires(bean = MongoClientFactoryForTest::class)
    abstract class IntegrationTestV1(body: FunSpec.() -> Unit = {}) : FunSpec(body) {
    

    Micronaut-data team or Micronaut-test team, needs a comment :D