Why does my flapdoodle Embedded MongoDB test fail

2020-08-13 07:45发布

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>

6条回答
▲ chillily
2楼-- · 2020-08-13 08:00

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

查看更多
做个烂人
3楼-- · 2020-08-13 08:03

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=
查看更多
forever°为你锁心
4楼-- · 2020-08-13 08:04

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.

查看更多
你好瞎i
5楼-- · 2020-08-13 08:07

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 {

}
查看更多
家丑人穷心不美
6楼-- · 2020-08-13 08:07

Try to add @DirtiesContext to the test class level.

查看更多
Summer. ? 凉城
7楼-- · 2020-08-13 08:22

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.

查看更多
登录 后发表回答