@datamongotest()不工作-引发错误的原因:org.springframework.beans.factory.nosuchbeandefinitionexception

evrscar2  于 2021-07-13  发布在  Java
关注(0)|答案(1)|浏览(416)

我使用的是mongodb事务特性,我使用的是springboot 2.3.5.RELEASE . 我正在为集成测试的控制器类编写单元测试。我面临以下例外。请帮帮我。告诉我哪里做错了。

  1. 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)}
  2. at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1717) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
  3. at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1273) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
  4. at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
  5. at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640) ~[spring-beans-5.2.10.RELEASE.jar:5.2.10.RELEASE]
  6. ... 71 common frames omitted

我提供以下相关代码。
下面是配置类。

  1. @EnableAutoConfiguration(exclude={ SecurityAutoConfiguration.class, DataSourceAutoConfiguration.class,
  2. RedisAutoConfiguration.class,
  3. RedisRepositoriesAutoConfiguration.class, MongoAutoConfiguration.class,
  4. SecurityFilterAutoConfiguration.class, SecurityAutoConfiguration.class })
  5. @SpringBootApplication(scanBasePackages = { "com.app.rain"}, exclude={SecurityAutoConfiguration.class})
  6. public class Test1Config {
  7. }

下面是控制器的测试。

  1. @ActiveProfiles("test")
  2. @DataMongoTest()
  3. //@ExtendWith(SpringExtension.class)
  4. //@ContextConfiguration(classes = ValidationApplication.class)
  5. @ContextConfiguration(classes = {
  6. Test1Config.class,
  7. })
  8. @ImportAutoConfiguration(TransactionAutoConfiguration.class)
  9. public class Test0 {
  10. @Autowired
  11. private ValidationCategoryController controller;
  12. @Autowired
  13. @Qualifier("validations")
  14. private ValidationService vldnService;
  15. @Test
  16. void testAllValidationsBeforeEntry() {
  17. System.out.println("controller : " + controller);
  18. assertEquals(true, true);
  19. }
  20. }

请帮我解决。我也看到这个@datamongotest失败是因为不满意的dependencyException。对此没有答案。

dl5txlt9

dl5txlt91#

经过几个小时的努力,我终于找到了解决办法。它可能对某人有帮助。我把密码贴在这里。

  1. @DataMongoTest(excludeAutoConfiguration = {SecurityAutoConfiguration.class,
  2. RedisAutoConfiguration.class, MongoDBTxnConfiguration.class,
  3. RedisRepositoriesAutoConfiguration.class,
  4. SecurityFilterAutoConfiguration.class,
  5. SecurityAutoConfiguration.class})
  6. @Profile("test")
  7. @ActiveProfiles("test")
  8. @ComponentScan(basePackages = {"com.app.rain"}, excludeFilters={
  9. @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE,
  10. value= {MongoDBTxnConfiguration.class,SecurityCloudConfig.class})})
  11. @Import(TestMongoDBConfig.class)
  12. public class Test0 {
  13. @Autowired
  14. private ValidationCategoryController controller;
  15. @Autowired
  16. @Qualifier("validations")
  17. private ValidationService vldnService;
  18. @Test
  19. void testAllValidationsBeforeEntry() {
  20. System.out.println("controller : " + controller);
  21. assertEquals(true, true);
  22. }
  23. }

班级 TestMongoDBConfig 课堂看起来像这样。

  1. @Profile("test")
  2. @ActiveProfiles("test")
  3. @Configuration
  4. public class TestMongoDBConfig implements InitializingBean, DisposableBean {
  5. private MongodExecutable executable;
  6. @Override
  7. public void afterPropertiesSet() throws Exception {
  8. IFeatureAwareVersion version = de.flapdoodle.embed.mongo.distribution.Versions
  9. .withFeatures(new GenericVersion("4.0.0"), Version.Main.PRODUCTION.getFeatures());
  10. IMongoCmdOptions cmdOptions = new MongoCmdOptionsBuilder().useNoPrealloc(false).useSmallFiles(false)
  11. .master(false).verbose(false).useNoJournal(false).syncDelay(0).build();
  12. int port = Network.getFreeServerPort();
  13. IMongodConfig mongodConfig = new MongodConfigBuilder().version(version)
  14. .net(new Net(port, Network.localhostIsIPv6())).replication(new Storage(null, "testRepSet", 5000))
  15. // .configServer(true)
  16. .configServer(false).cmdOptions(cmdOptions).build();
  17. MongodStarter starter = MongodStarter.getDefaultInstance();
  18. executable = starter.prepare(mongodConfig);
  19. executable.start();
  20. }
  21. @Primary
  22. @Bean(name = "test1")
  23. public MongoClient mongoClient() {
  24. MongoClient mongoClient = MongoClients.create();
  25. return mongoClient;
  26. }
  27. @Override
  28. public void destroy() throws Exception {
  29. executable.stop();
  30. }
  31. }
展开查看全部

相关问题