PythonLearner
PythonLearner

Reputation: 1466

@DataMongoTest() is not working - throwing Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException

I am using MongoDB transaction feature, I am using Spring Boot 2.3.5.RELEASE. I am writing unit test for controller classes for integration test. I am facing the following exception. Please help me. Let me know where I am doing wrong.

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.app.rain.resources.ValidationCategoryController' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1717) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1273) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
    ... 71 common frames omitted

I provide below the relevant code.

Below is the configuration class.

@EnableAutoConfiguration(exclude={ SecurityAutoConfiguration.class, DataSourceAutoConfiguration.class, 
        RedisAutoConfiguration.class,
RedisRepositoriesAutoConfiguration.class, MongoAutoConfiguration.class, 
        SecurityFilterAutoConfiguration.class, SecurityAutoConfiguration.class })
@SpringBootApplication(scanBasePackages = { "com.app.rain"}, exclude={SecurityAutoConfiguration.class})
public class Test1Config {

}

Below is the Test for the controller.

@ActiveProfiles("test")
@DataMongoTest()
//@ExtendWith(SpringExtension.class)
//@ContextConfiguration(classes = ValidationApplication.class)

@ContextConfiguration(classes = {
        Test1Config.class,
    })
@ImportAutoConfiguration(TransactionAutoConfiguration.class)
public class Test0 {
    @Autowired
    private ValidationCategoryController controller;
    
    @Autowired
    @Qualifier("validations")
    private ValidationService vldnService;
    
    @Test
    void testAllValidationsBeforeEntry() {
        System.out.println("controller : " + controller);
        assertEquals(true, true);
    }
}

Please help me to resolve. I have also seen this @DataMongoTest fails because of UnsatisfiedDependencyException. There is no answer for that.

Upvotes: 2

Views: 1909

Answers (1)

PythonLearner
PythonLearner

Reputation: 1466

After struggling for many hours, I finally found the solution. It may be helpful for someone. I post the code here.

@DataMongoTest(excludeAutoConfiguration = {SecurityAutoConfiguration.class,
        RedisAutoConfiguration.class, MongoDBTxnConfiguration.class,
        RedisRepositoriesAutoConfiguration.class, 
        SecurityFilterAutoConfiguration.class, 
        SecurityAutoConfiguration.class})
@Profile("test")
@ActiveProfiles("test")
@ComponentScan(basePackages = {"com.app.rain"}, excludeFilters={
          @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, 
                  value= {MongoDBTxnConfiguration.class,SecurityCloudConfig.class})})
@Import(TestMongoDBConfig.class)
public class Test0 {
    @Autowired
    private ValidationCategoryController controller;
    
    @Autowired
    @Qualifier("validations")
    private ValidationService vldnService;
    
    @Test
    void testAllValidationsBeforeEntry() {
        System.out.println("controller : " + controller);
        assertEquals(true, true);
    }
}

The class TestMongoDBConfig class looks like this.

@Profile("test")
@ActiveProfiles("test")
@Configuration
public class TestMongoDBConfig implements InitializingBean, DisposableBean {

    private MongodExecutable executable;

    
    @Override
    public void afterPropertiesSet() throws Exception {

        IFeatureAwareVersion version = de.flapdoodle.embed.mongo.distribution.Versions
                .withFeatures(new GenericVersion("4.0.0"), Version.Main.PRODUCTION.getFeatures());
        IMongoCmdOptions cmdOptions = new MongoCmdOptionsBuilder().useNoPrealloc(false).useSmallFiles(false)
                .master(false).verbose(false).useNoJournal(false).syncDelay(0).build();
        int port = Network.getFreeServerPort();
        IMongodConfig mongodConfig = new MongodConfigBuilder().version(version)
                .net(new Net(port, Network.localhostIsIPv6())).replication(new Storage(null, "testRepSet", 5000))
//            .configServer(true)
                .configServer(false).cmdOptions(cmdOptions).build();
        MongodStarter starter = MongodStarter.getDefaultInstance();
        executable = starter.prepare(mongodConfig);
        executable.start();
    }

    
    @Primary
    @Bean(name = "test1")
    public MongoClient mongoClient() {
        MongoClient mongoClient = MongoClients.create();
        return mongoClient;
    }

    @Override
    public void destroy() throws Exception {
        executable.stop();
    }
}

Upvotes: 2

Related Questions