MappingMongoConverter not available for autowiring on unit test with @EnableMongoAuditing

Viewed 995

I have a spring boot application where I have enabled mongo auditing. The application starts fine and stores mongo documents with all auditing fields (createdDate, updatedDate, etc.). However, when running unit test I get the following exception:

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoAuditingHandler': Cannot create inner bean '(inner bean)#115c946b' of type [org.springframework.data.mongodb.config.MongoAuditingRegistrar$MongoMappingContextLookup] while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#115c946b': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.convert.MappingMongoConverter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#115c946b': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.convert.MappingMongoConverter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.data.mongodb.core.convert.MappingMongoConverter' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}

Here is my main class

@SpringBootApplication
@EnableMongoAuditing
public class TestApplication {
    public static void main(String[] args) {
        SpringApplication.run(PatientstoreApplication.class, args);
    }
}

And here is my test class:

@WebMvcTest(TestController.class)
public class TestTest {
    @Autowired MockMvc mockMvc;
    @MockBean TestService testService;

    @Test
    public void test() {

    }
}

A reproducible example can be found here: https://github.com/jota87r/testapp

My question is: is there any configuration missing? should I manually create a bean of type for MappingMongoConverter for my test cases?

2 Answers

You can mock the MappingMongoConverter.class.

So your test class should be as follows:

@WebMvcTest(TestController.class)
public class TestTest {

@Autowired
MockMvc mockMvc;

@MockBean
TestService testService;

//Mock MappingMongoConverter 
@MockBean
private MappingMongoConverter mappingMongoConverter;

@Test
public void test() {

}

I encountered the same situation.

In your @WebMvcTest class, try to add a test configuration like:

    @TestConfiguration
    static class TestConfig {
        @Bean
        public MappingMongoConverter mongoConverter() {
            return new MappingMongoConverter(mock(DbRefResolver.class), mock(MappingContext.class));
        }
    }
Related