how to set up an embedded MongoDB with a docker image

Viewed 457

I have the following lines of code that is intended to set up an embedded mongodb with a docker image

    @Container
    public static GenericContainer MONGO_CONTAINER = new GenericContainer<>("URL_OF_DOCKER")
            .withExposedPorts(27017);

    static class Initializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {
        public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
            final String propertyOverride = format("mongo.servers: %s:%s", MONGO_CONTAINER.getContainerIpAddress(), MONGO_CONTAINER.getMappedPort(27017));
            TestPropertyValues.of(propertyOverride).applyTo(configurableApplicationContext.getEnvironment());
        }
    }

However, I am unable to use the reactivemongotemplate to save documents. I am getting the following error:

Caused by: org.springframework.dao.DataAccessResourceFailureException: Timed out after 2000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}]; nested exception is com.mongodb.MongoTimeoutException: Timed out after 2000 ms while waiting for a server that matches WritableServerSelector. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connection refused}}]

Would anyone know the issue?

0 Answers
Related