如何对自定义mongo存储库进行集成测试?

时间:2019-05-06 10:00:00

标签: spring-boot spring-data-jpa spring-data-mongodb spring-properties

这些是我在项目中完成的以下配置。

    @Configuration

    public class AppMongoConfig {

      @Autowired private MongoDbFactory mongoDbFactory;

      @Autowired private MongoMappingContext mongoMappingContext;

      @Bean
      public MappingMongoConverter mappingMongoConverter() {

        DbRefResolver dbRefResolver = new DefaultDbRefResolver(mongoDbFactory);
        MappingMongoConverter converter = new MappingMongoConverter(dbRefResolver, mongoMappingContext);
        converter.setTypeMapper(new DefaultMongoTypeMapper(null));

        return converter;
      }
    }
@Configuration
@RequiredArgsConstructor
@EnableConfigurationProperties(MultipleMongoProperties.class)
public class MultipleMongoConfig {

  private final MultipleMongoProperties mongoProperties;

  @Primary
  @Bean(name = "primaryMongoTemplate")
  public MongoTemplate primaryMongoTemplate() throws Exception {
    return new MongoTemplate(primaryFactory(this.mongoProperties.getPrimary()));
  }

  @Bean(name = "secondaryMongoTemplate")
  public MongoTemplate secondaryMongoTemplate() throws Exception {
    return new MongoTemplate(secondaryFactory(this.mongoProperties.getSecondary()));
  }

  @Bean
  @Primary
  public MongoDbFactory primaryFactory(final MongoProperties mongo) throws Exception {
    return new SimpleMongoDbFactory(new MongoClientURI(mongo.getUri()));
  }

  @Bean
  public MongoDbFactory secondaryFactory(final MongoProperties mongo) throws Exception {
    return new SimpleMongoDbFactory(new MongoClientURI(mongo.getUri()));
  }
}


@Data
@ConfigurationProperties(prefix = "mongodb")
public class MultipleMongoProperties {
    private MongoProperties primary = new MongoProperties();
    private MongoProperties secondary = new MongoProperties();
}


@Configuration
@EnableMongoRepositories(
    basePackages = {"com.student.repository.primary"},
    mongoTemplateRef = "primaryMongoTemplate")
public class PrimaryMongoConfig {}

@Configuration
@EnableMongoRepositories(
    basePackages = {"com.student.repository.secondary"},
    mongoTemplateRef = "secondaryMongoTemplate")
public class SecondaryMongoConfig {}

存储库代码:

 public interface StudentDAO {

      Student save(StudentInfo studentInfo); 
} 

@Repository 
public class StudentDAOImpl implements StudentDAO {

      @Autowired   @Qualifier("primaryMongoTemplate")   
      private MongoTemplate mongoTemplate;

      @Override   public StudentInfo save(StudentInfo userWatchlist) {
        return mongoTemplate.save(userWatchlist);   
     } 
}

集成测试代码:

@RunWith(SpringRunner.class)
@DataMongoTest(includeFilters = @Filter(Repository.class))
public class WatchListDAOImplIT {

  @Autowired private StudentDAO studentDAO;

  @Test
  public void save() {
    StudentInfo studentInfo = getStudentInfo();
    StudentInfo dbUserWatchlist = watchListDAO.save(studentInfo);
    Assert.assertEquals(studentInfo.getId(), dbUserWatchlist.getId());
  }

  private StudentInfo getStudentInfo() {
    StudentInfo studentInfo = new StudentInfo();
    studentInfo.setId(9999999l);
    return studentInfo;
  }
}

哪个给我以下错误:-

  

java.lang.IllegalStateException:无法加载ApplicationContext

     在

  org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)     在   org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:108)     在   org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:   :org.springframework.beans.factory.UnsatisfiedDependencyException:   创建名称为“ appMongoConfig”的bean时出错:不满意的依赖关系   通过字段“ mongoDbFactory”表示;嵌套异常为   org.springframework.beans.factory.UnsatisfiedDependencyException:   创建在类路径中定义的名称为“ primaryFactory”的bean时出错   资源[... / config / mongo / MultipleMongoConfig.class]:不满意   通过方法'primaryFactory'参数0表示的依赖关系;   嵌套异常为   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   类型的合格豆   'org.springframework.boot.autoconfigure.mongo.MongoProperties'   可用:至少有1个符合自动装配条件的bean   候选人。依赖项注释:{}

     

原因:   org.springframework.beans.factory.NoSuchBeanDefinitionException:否   类型的合格豆   'org.springframework.boot.autoconfigure.mongo.MongoProperties'   可用:预计至少有1个豆

请对此提供帮助。

0 个答案:

没有答案