u123
u123

Reputation: 16297

Spring boot tries to connect to mongo when adding mongo-java-driver maven dependency

I just added:

    <dependency>
        <groupId>org.mongodb</groupId>
        <artifactId>mongo-java-driver</artifactId>
    </dependency>

to my maven spring boot project. And I also have this test:

/*
 * Copyright 2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package hello;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
public class GreetingControllerTests {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void noParamGreetingShouldReturnDefaultMessage() throws Exception {

        this.mockMvc.perform(get("/greeting")).andDo(print()).andExpect(status().isOk())
                .andExpect(jsonPath("$.content").value("Hello, World!"));
    }

    @Test
    public void paramGreetingShouldReturnTailoredMessage() throws Exception {

        this.mockMvc.perform(get("/greeting").param("name", "Spring Community"))
                .andDo(print()).andExpect(status().isOk())
                .andExpect(jsonPath("$.content").value("Hello, Spring Community!"));
    }

}

For some reason when I build my project with mvn clean package spring boot now tries connect to a mongodb:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.7.RELEASE)

2019-10-11 20:36:58.418  INFO 14870 --- [           main] hello.GreetingControllerTests            : Starting GreetingControllerTests on user-ThinkPad-X390 with PID 14870 (started by user in /home/user/repos/frontend-backend/backend)
2019-10-11 20:36:58.423  INFO 14870 --- [           main] hello.GreetingControllerTests            : No active profile set, falling back to default profiles: default
2019-10-11 20:36:59.311  INFO 14870 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2019-10-11 20:36:59.565  INFO 14870 --- [           main] org.mongodb.driver.cluster               : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms', maxWaitQueueSize=500}
2019-10-11 20:36:59.591  INFO 14870 --- [localhost:27017] org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server localhost:27017

com.mongodb.MongoSocketOpenException: Exception opening socket
    at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:67) ~[mongo-java-driver-3.8.2.jar:na]
    at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:126) ~[mongo-java-driver-3.8.2.jar:na]
    at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:117) ~[mongo-java-driver-3.8.2.jar:na]
    at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]
Caused by: java.net.ConnectException: Connection refused (Connection refused)
    at java.base/java.net.PlainSocketImpl.socketConnect(Native Method) ~[na:na]
    at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:399) ~[na:na]

If I just delete the test or the mongo dependency the above mongo connection is not initiated.

But there is no reference to mongo in the above test so why does spring try to start a mongo connection?

Upvotes: 1

Views: 1443

Answers (1)

pvpkiran
pvpkiran

Reputation: 27058

If I have to answer in one sentence, this is because Springboot is Opinionated. It will try to connect to mongo as soon as it notice mongo dependency in your pom through AutoConfiguration classes.

If you want to override the default behaviour and tell Springboot not to do MongoAutoConfiguration then you can do it this way

@SpringBootApplication(exclude=MongoAutoConfiguration.class)
public class YourMainApplication {

    public static void main(String[] args) {
        SpringApplication.run(TestApplication.class, args);
    }
}

or you can do it with this line in your properties file

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration

If you do either of the above, then it will exclude MongoAutoconfiguration from your application(not just from your tests). Which means when you start your application then you do not have access to mongo(If this is what you desire).

Since SpringbootTest annotation loads the whole application cotext, it looks for this Main Application class. If you have some Autoconfigurations excluded then it will exclude, even in your tests. So you would not have the connecting to mongo issue.

If you wish to exclude this Autoconfiguration only in tests(so that while running your application nothing is changed), you can do it this way

@TestPropertySource(properties=
{"spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration"})
@SpringBootTest
public class GreetingControllerTests {...}

Upvotes: 2

Related Questions