Why does my flapdoodle Embedded MongoDB test fail to run? (creating 'embeddedMongoServer' could not start process EOF)
Asked Answered
A

14

23

I'm having trouble getting my brand new project to build. I used https://start.spring.io/ to generate a fresh new Spring 2.0 MongoDB Maven project, and I want to have an embedded MongoDB database for my integration tests. The spring initializer added a dependency for de.flapdoodle.embed.mongo to that end.

But every time I try to run a "mvn clean package", I get the following error during my test:

Caused by: org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'embeddedMongoServer' defined in class path resource
[org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: 
Invocation of init method failed; nested exception is java.io.IOException: 
Could not start process: <EOF>
at de.flapdoodle.embed.mongo.AbstractMongoProcess.onAfterProcessStart(AbstractMongoProcess.java:79) ~[de.flapdoodle.embed.mongo-2.0.3.jar:na]
at de.flapdoodle.embed.process.runtime.AbstractProcess.<init>(AbstractProcess.java:116) ~[de.flapdoodle.embed.process-2.0.2.jar:na]
at de.flapdoodle.embed.mongo.AbstractMongoProcess.<init>(AbstractMongoProcess.java:53) ~[de.flapdoodle.embed.mongo-2.0.3.jar:na]
at de.flapdoodle.embed.mongo.MongodProcess.<init>(MongodProcess.java:50) ~[de.flapdoodle.embed.mongo-2.0.3.jar:na]
at de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:44) ~[de.flapdoodle.embed.mongo-2.0.3.jar:na]
at de.flapdoodle.embed.mongo.MongodExecutable.start(MongodExecutable.java:34) ~[de.flapdoodle.embed.mongo-2.0.3.jar:na]
at de.flapdoodle.embed.process.runtime.Executable.start(Executable.java:108) ~[de.flapdoodle.embed.process-2.0.2.jar:na]

What am I missing?

My Application file is pretty straightforward:

@SpringBootApplication
public class NewnewinternetApplication {

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

My Config file is very simple:

@Configuration
@EnableMongoRepositories
@ComponentScan(basePackages = "com.snoop.dougg.newnewinternet")
public class AppConfig {

    @Bean
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/");
        resolver.setSuffix(".html");
        return resolver;
    }
}

I have two simple controllers returning just static output for now.

I have a little document:

@Document(collection = "user")
public class User implements Serializable {
    protected static final long serialVersionUID = -1L;

    @Id
    private String id;

    private String username;
    private String firstName;
    private String lastName;

    public User() {}

    public User(String username, String firstName, String lastName) {
        this.username = username;
        this.firstName = firstName;
        this.lastName = lastName;
    }

    //Getters, setters, and equals and hash code methods...
}

And then a silly little test:

@RunWith(SpringRunner.class)
//@SpringBootTest -> Doesn't work either
@DataMongoTest
public class NewnewinternetApplicationTests {

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void sillyLittleTest() {
        mongoTemplate.save(new User("sdoug", "Snoop", "Dougg"));
        Assert.notNull(
            mongoTemplate.find(
                new Query(Criteria.where("firstName").is("Snoop")), User.class),
            "Couldn't find by first name!");
    }
}

And then my pom file, which I really just left alone:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.snoop.dougg.newnewinternet</groupId>
    <artifactId>NewNewInternet</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>

    <name>NewNewInternet</name>
    <description>A new new internet</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.1.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <azure.version>2.0.1</azure.version>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.M9</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-active-directory-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-keyvault-secrets-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>com.microsoft.azure</groupId>
            <artifactId>azure-spring-boot</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-core</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <dependency>
                <groupId>com.microsoft.azure</groupId>
                <artifactId>azure-spring-boot-bom</artifactId>
                <version>${azure.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>https://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>


</project>
Annalist answered 23/4, 2018 at 21:36 Comment(0)
K
7

Commenting out the following lines in application.properties and placing them in a different profile can also work. I found it here

spring.data.mongodb.database=
spring.data.mongodb.host=
spring.data.mongodb.port=
Karyokinesis answered 15/5, 2020 at 22:0 Comment(1)
This causes a Database name must not be empty! error log in my project.Artemis
P
6

I was in the same situation, and I could resolve it using @DirtiesContext on this way:

@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
public class CommoditiesApplicationTests {

}
Parishioner answered 19/9, 2019 at 9:44 Comment(1)
It doesn't help as well.Indemnification
F
5

my error message was exactly like this

2022-03-15 10:57:00.053  WARN 7196 --- [    Test worker] s.c.a.AnnotationConfigApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'embeddedMongoServer' defined in class path resource [org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Unsatisfied dependency expressed through method 'embeddedMongoServer' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'embeddedMongoConfiguration' defined in class path resource [org/springframework/boot/autoconfigure/mongo/embedded/EmbeddedMongoAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [de.flapdoodle.embed.mongo.config.MongodConfig]: Factory method 'embeddedMongoConfiguration' threw exception; nested exception is java.lang.IllegalStateException: Set the spring.mongodb.embedded.version property or define your own MongodConfig bean to use embedded MongoDB

so, I add property in my application.yml file.

spring.mongodb.embedded.version: 3.2.3

and, solved it.

Febrile answered 15/3, 2022 at 2:9 Comment(2)
This one worked for me... I don't know why, but it worked. Thank you.Alcala
This seems to have worked for me too... I also don't understand why.Arsenopyrite
Z
4

In my case the 32 bit mongodb client was downloaded instead of the 64 bit one. embedded.mongo library uses BitSize class to determine the OS architecture. In my system System.getProperty("os.arch") was not returning a value listed in the if statement. I solved the problem by setting os.arch system property to x86_64 (one of the values used by BitSize to return B64) in my application main.

@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        System.setProperty("os.arch", "x86_64");
        SpringApplication.run(Application.class, args);
    }
}

Note: System.getProperty("os.arch") will return the wrong value if you use a 32 bit java version to run your application on a 64 bit system!

Zachariahzacharias answered 10/4, 2021 at 13:31 Comment(1)
I needed to add the below on @BeforeAll to make the tests pass on my m1 mac: System.setProperty("os.arch", "x86_64");Lotson
P
3

Usually already running mongodb instance is the source of the issue. I would start with checking if anything occupies default mongodb port - 27017.

Parboil answered 16/9, 2020 at 13:44 Comment(0)
S
2

chances are the instance of mongodb downloaded through the spring plugin is 32 & you are running on 64 bit java or vice versa. Please confirm if there is any other way you have identified the fix.

Scrooge answered 7/7, 2018 at 7:12 Comment(0)
T
1

I had the pretty same scenario here, and solved it using

    <dependency>
        <groupId>com.github.fakemongo</groupId>
        <artifactId>fongo</artifactId>
        <version>2.1.1</version>
        <scope>test</scope>
    </dependency>

instead of de.flapdoodle.embed.mongo

Terpsichorean answered 11/12, 2019 at 0:40 Comment(0)
A
1

In my case, the socket file was still around.

To get to the underlying issue, I wanted the console logging output, I put a breakpoint in the else clause of the AbstractMongoProcess::onAfterProcessStart (which is hit on failure). Here you have access to the logWatch and can run a System.out.println(logWatch.output.toString()); in debug mode to get the mongo console out. For my issue, the output said SocketException: Address already in use

Trying commands suggested such as sudo lsof -iTCP -sTCP:LISTEN -n -P did not work for me (nothing listed in my case)

I found another SO answer that said to run ls -lrta /tmp | grep .sock

The .sock file was still there from a previous run (Apparently I had interrupted my tests)

Deleting this file solved the issue.

Annatto answered 16/12, 2020 at 14:19 Comment(0)
N
1

The error is due to the package de.flapdoodle.embed which was used for Embedded Mongo, use a stable version of it 3.5.0

    <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <version>3.5.0</version>
            <scope>test</scope>
        </dependency>

add it in the pom.file(add the version to it).Then update maven it should work fine

Nascent answered 17/10, 2022 at 8:33 Comment(0)
S
0

My case was a bit special, but maybe this help someone else too to resolve this.

If, by any chance, you are using win 10 and you have already a MongoDB running as a service (in my case it was an earlier version - v3.4 - running), then try to stop the service, and run the test afterwards.

Stereophonic answered 23/8, 2018 at 20:6 Comment(0)
T
0

Try to add @DirtiesContext to the test class level.

Telegraphic answered 10/5, 2019 at 4:21 Comment(2)
It doesn't help.Indemnification
It does not workEndorse
C
0

I deleted the 'mongo' dir in my appdata/temp and that is when I caught my McAfee quarantining my embedded mongo. I turned OFF McAfee and deleted the temp mongo again and then all ran great...

Correlative answered 29/11, 2021 at 17:33 Comment(0)
L
0

Same problem. spring.data.mongodb.port was 27017 in application.properties. I changed it to 0. When 0 is used, a random port is assigned instead.

My Integration Test is like below:

        @RunWith(SpringRunner.class)
        @DataMongoTest
        public class IntegrationTestIT { ... }

I'm using de.flapdoodle.embed:de.flapdoodle.embed.mongo:3.4.6.

Lichfield answered 30/8, 2022 at 13:55 Comment(0)
F
0

I had the same issue. Refer to this github issue for the solution if your problem is related to flapdoodle: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/427

Just increase the flapdoodle version >=3.5.0 and if possible increase also spring-boot version to the latest

Fusiform answered 2/11, 2022 at 10:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.