I'm building a Spring Boot application that uses Spring Data Repositories with MongoDB. I'm attempting to create a Spock functional Spec to test my repository but I can't figure out the appropriate way to initialize the Mongo DB in preparation for testing. So far I have tried the following:
- Do Nothing - This resulted in the same database being used from test to test with my tests failing after.
- Drop the database before testing - This resulted in the indexes being lost and me being unable to test my unique indexes.
Here's what I was doing with dropping the database:
@ContextConfiguration(classes = MyApp, loader = SpringApplicationContextLoader)
@ActiveProfiles('test')
class UserRepositoryTest extends Specification {
@Shared
boolean mongoReset = false
@Autowired
MongoTemplate mongoTemplate
@Autowired
UserRepository userRepository
void setup() {
if (!mongoReset) {
mongoTemplate.getDb().dropDatabase()
mongoReset = true
}
}
}
Ideally I'd like to be able to use something similar to the data.sql method provided with JPA repositories.