Skip to content

Instantly share code, notes, and snippets.

@nsdiv
nsdiv / gist:9effb78e27ff6ca94381
Created November 7, 2015 00:37
Mock MongoDB when unit testing services with Spring Boot
If you are using Spring Boot with MongoDB, then a MongoDB connection is required even if you are unit testing a function.
Spring creates a connection to mongoDB during start up, because of Autowired beans in your code. This slows down your unit tests, and
also means your unit tests require access to the MongoDB server.
Here is what you need to do:
1. Mock the beans created by org.springframework.boot.autoconfigure.data.mongo.MongoDataAutoConfiguration:
@Bean
public MongoDbFactory mongoDbFactory(){
return null;
}