Is there a way to deactivate the connection between the Java application and the mongo database to see what exceptions are thrown at run time?

Viewed 71

I am trying to understand how I can shutdown the connection to mongo while my java application runs for tesing purposes. I am trying to see how my application would behave in case mongo is unavailable. Is there any way this accomplished at the application level without touching the mongo database?. This is the class I use to create the mongo connection

@Configuration
@EnableMongoAuditing
public class MongoConfiguration {
    @Autowired
    private MongoProperties mongoProperties;

    @Bean
    public MongoClient mongo(){
        ConnectionString connectionString = new ConnectionString(mongoProperties.getUri());
        MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
                .applyConnectionString(connectionString)
                .build();
        return MongoClients.create(mongoClientSettings);
    }
    // to use @Transactional -- supporting spring data transaction support in MongoDB
    @Bean
    MongoTransactionManager transactionManager(MongoDbFactory dbFactory){
        return  new MongoTransactionManager(dbFactory);
    }

    @Primary
    @Bean(name = "mongoTemplate")
    public MongoTemplate mongoTemplate(){
        return new MongoTemplate(mongo(), mongoProperties.getDatabase());
    }

}
1 Answers
Related