Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear mongodb using spring-mongodb

As so many people before me on stackoverflow I want to do junit-tests for my mongodb application. For this I let an instance run, so my tests can connect. But I want to ensure that the database is in an empty state when starting to test.

Since I'm using spring-mongodb, spring-test and all the funny spring-things I hoped to find a configuration that does this for me, until now successless.

Can anybody give me a hint how to implement this in a clean way. My idea is a InitializingBean that does this for me. But I'm not really happy with ths solution. Any suggestions?

like image 290
mibutec Avatar asked Jan 12 '13 17:01

mibutec


1 Answers

Try this:

@Autowired
org.springframework.data.mongodb.core.MongoTemplate mongoTemplate;

.....
mongoTemplate.getDb().dropDatabase();
like image 109
Gennady Shumakher Avatar answered Sep 30 '22 07:09

Gennady Shumakher