0

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:

  1. Do Nothing - This resulted in the same database being used from test to test with my tests failing after.
  2. 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.

1 Answer 1

3

We usually recommend to rather use the repository to wipe the database (i.e. calling userRepository.deleteAll()). Dropping the database has the downside of wiping all the indexes that might have been created during context bootstrap time.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.