I assume that (and I mean, this is what I did, but there is every chance that someone has a better idea) write some kind of MongoTestHelper that can do several things at different stages of your tests.
Before starting the test, he checks that the test mongod instance is working, and, if not, downloads it to his favorite test mongo port. I believe that this is actually not so expensive, just try and download a new instance of mongod and let it fail, because this port is already in use. However, this is very different from windows, so you can check that the port is open or something like that.
Before each individual test, you can remove all items from all verified collections, if that is what you need. In fact, I'm just throwing all the databases, as the beautiful mongodb recreates them for you:
for (String name : mongo.getDatabaseNames()) { mongo.dropDatabase(name); }
After running the tests, you can always close it if you chose to boot on a random port, but that seems a little silly. Life is too short.
source share