0

我不仅将 Fongo 用于单元测试,还用于集成测试,所以我想用一些集合来初始化 Fongo,这可能吗?

这是我的 java 配置(基于 Oliver G. answer):

@EnableAutoConfiguration(exclude = { 
    EmbeddedMongoAutoConfiguration.class,
    MongoAutoConfiguration.class,
    MongoDataAutoConfiguration.class
})
@Configuration
@ComponentScan(basePackages = { "com.foo" },
    excludeFilters = { @ComponentScan.Filter(classes = { SpringBootApplication.class }) 
})
public class ConfigServerWithFongoConfiguration extends AbstractFongoBaseConfiguration {

    private static final Logger log = LoggerFactory.getLogger(ConfigServerWithFongoConfiguration.class);

    @Autowired
    ResourcePatternResolver resourceResolver;

    @Bean
    public Jackson2RepositoryPopulatorFactoryBean repositoryPopulator() {

        Jackson2RepositoryPopulatorFactoryBean factory = new Jackson2RepositoryPopulatorFactoryBean();
        try {
            factory.setResources(resourceResolver.getResources("classpath:static/collections/*.json"));
        } catch (IOException e) {
            log.error("Could not load data", e);
        }
        return factory;
    }

}

当我运行 IT 测试时,它会出现在日志上,Reading resource: file *.json但测试失败,因为它们从 Fongo 数据库中没有检索到任何内容(空)。

测试注释为:

@RunWith(SpringRunner.class)
@SpringBootTest(classes={ConfigServerWithFongoConfiguration.class})
@AutoConfigureMockMvc
@TestPropertySource(properties = {"spring.data.mongodb.database=fake"})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
4

1 回答 1

0

大声笑,我现在觉得很愚蠢。是格式问题。JSON 集合的格式必须如下:

[
  {/*doc1*/},
  {/*doc2*/},
  {/*doc3*/}
]

我错过了[]逗号分隔的文件。

于 2017-06-02T07:57:04.570 回答