0

I'm using Fongo not only for unit tests but also for integration tests so I would like to initialize Fongo with some collections, is that possible?

This is my java config (based on 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;
    }

}

When I run my IT tests, on the log it appears Reading resource: file *.json but the tests fails because they retrieve nothing (null) from Fongo database.

Tests are annotated with:

@RunWith(SpringRunner.class)
@SpringBootTest(classes={ConfigServerWithFongoConfiguration.class})
@AutoConfigureMockMvc
@TestPropertySource(properties = {"spring.data.mongodb.database=fake"})
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_CLASS)
pvpkiran
  • 25,582
  • 8
  • 87
  • 134
anat0lius
  • 2,145
  • 6
  • 33
  • 60
  • Have you though of using https://github.com/lordofthejars/nosql-unit. With this it is much more simpler to initialize database with sample data. here is an example as well https://ivanursul.com/spring-data-mongo-testing-using-in-memory-db/ – pvpkiran Jun 02 '17 at 07:35

1 Answers1

0

Lol, I feel so stupid right now. Was format issue. JSON collections must be formated like this:

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

I was missing the [] and comma separated documents.

anat0lius
  • 2,145
  • 6
  • 33
  • 60