2

I have an example spring boot project which uses mongo db for persisting objects. I would like to test it with an embedded mongo db but I get an error which is very similar to this question (see comments of the accepted answer): For some reason IFeatureAwareVersion from flapdoodle cannot be found.

Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.data.mongodb.core.MongoTemplate]: Factory method 'mongoTemplate' threw exception; nested exception is java.lang.NoClassDefFoundError: de/flapdoodle/embed/mongo/distribution/IFeatureAwareVersion
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:579) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    ... 65 common frames omitted
Caused by: java.lang.NoClassDefFoundError: de/flapdoodle/embed/mongo/distribution/IFeatureAwareVersion
    at cz.jirutka.spring.embedmongo.EmbeddedMongoFactoryBean.<init>(EmbeddedMongoFactoryBean.java:47) ~[embedmongo-spring-1.3.1.jar:1.3.1]
    at com.example.MongoConfig.mongoTemplate(MongoConfig.java:37) ~[test-classes/:na]
    at com.example.MongoConfig$$EnhancerBySpringCGLIB$$108c2b8.CGLIB$mongoTemplate$0(<generated>) ~[test-classes/:na]
    at com.example.MongoConfig$$EnhancerBySpringCGLIB$$108c2b8$$FastClassBySpringCGLIB$$629c796a.invoke(<generated>) ~[test-classes/:na]
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228) ~[spring-core-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:361) ~[spring-context-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at com.example.MongoConfig$$EnhancerBySpringCGLIB$$108c2b8.mongoTemplate(<generated>) ~[test-classes/:na]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_92]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_92]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_92]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_92]
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    ... 66 common frames omitted
Caused by: java.lang.ClassNotFoundException: de.flapdoodle.embed.mongo.distribution.IFeatureAwareVersion
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381) ~[na:1.8.0_92]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424) ~[na:1.8.0_92]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) ~[na:1.8.0_92]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357) ~[na:1.8.0_92]
    ... 78 common frames omitted

Here is the pom

<?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.example</groupId>
    <artifactId>demo-mongodb</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>demo-mongodb</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.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>
        <java.version>1.8</java.version>
        <spring-cloud.version>Finchley.M8</spring-cloud.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
        </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>cz.jirutka.spring</groupId>
            <artifactId>embedmongo-spring</artifactId>
            <version>1.3.1</version>
            <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>
        </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>

The Spring boot start parent 2.0.0.RELEASE manages the version of flapdoodle which is 2.0.3 and I see the class IFeatureAwareVersion in the de.flatpdoodle.embed.mongo-2.0.3.jar.

When I look at the dependancy hierarchy I see

  • embedmongo-spring: 1.3.1 [test]

    • de.flatpdoodle.embed.mongo: 2.0.3 (managed from 1.46.4) (ommitted for conflict with 2.0.3) [test]
    • de.flatpdoodle.embed.process: 1.40.1 (ommitted for conflict with 2.0.2) [test]

Here is the test

@RunWith(SpringRunner.class)
@SpringBootTest(classes=MongoConfig.class)
@TestPropertySource(locations = "classpath:application-test.properties")
public class Test_TimezoneDao {

    private static final Logger LOG = LoggerFactory.getLogger(Test_TimezoneDao.class);

    //@Autowired private TimezoneDao dao;
    @Autowired private CounterService counterService;

    @Autowired
    private MongoTemplate mongoTemplate;

    @Test
    public void test() 
    {
        TimeZone tz = new TimeZone();
        tz.setId(counterService.getNextSequence());
        tz.setOffset(9);
        tz.setTz("Asia/Singapore");

        TimeZone tz2 = new TimeZone();
        tz2.setId(counterService.getNextSequence());
        tz2.setOffset(11);
        tz2.setTz("Australia/Sydney");

        mongoTemplate.save(tz);

        List<TimeZone> tzs = mongoTemplate.findAll(TimeZone.class, "timezones");

        for(TimeZone t : tzs)
        {
            LOG.info(t.toString());
        }
    }
}

and here is the test config

@SpringBootConfiguration
@Profile("test")
@ComponentScan(basePackages= {"com.example.demomongodb"})
@EnableJpaRepositories(basePackages= {"com.example.demomongodb"})
@EnableMongoRepositories(basePackages= {"com.example.demomongodb"})
@EntityScan(basePackages= {"com.example.demomongodb"})
@EnableAutoConfiguration
public class MongoConfig 
{
    public MongoConfig()
    {
        System.out.println("============= LOADING MONGO CONFIG ===============");
    }

    @Bean
    public MongoTemplate mongoTemplate() throws IOException {
        EmbeddedMongoFactoryBean mongo = new EmbeddedMongoFactoryBean();
        mongo.setBindIp("localhost");
        MongoClient mongoClient = mongo.getObject();
        MongoTemplate mongoTemplate = new MongoTemplate(mongoClient, "test_or_whatever_you_want_to_call_this_db");
        return mongoTemplate;
    }
}

Thanks for the help

tenticon
  • 2,639
  • 4
  • 32
  • 76

2 Answers2

1

embedmongo-spring is kinda deprecated, it is embed mongo is now integrated directly into spring boot not via a third party dependency.

Just remove this dependency and you should be done.

You can also take a look at this issue, which explains the situation a bit further https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/260

Zarathustra
  • 2,853
  • 4
  • 33
  • 62
0

de.flapdoodle.embed dependency will be added through cz.jirutka.spring.

[INFO] +- cz.jirutka.spring:embedmongo-spring:jar:1.3.1:compile
[INFO] |  +- de.flapdoodle.embed:de.flapdoodle.embed.process:jar:1.40.1:compile
[INFO] |  |  +- net.java.dev.jna:jna:jar:4.5.2:compile
[INFO] |  |  \- net.java.dev.jna:jna-platform:jar:4.5.2:compile
[INFO] |  \- org.mongodb:mongo-java-driver:jar:3.11.2:compile
[INFO] +- de.flapdoodle.embed:de.flapdoodle.embed.mongo:jar:2.2.0:test

This issue is fixed by excluding the de.flapdoodle.embed.process from cz.jirutka.spring dependency.

        <dependency>
        <groupId>cz.jirutka.spring</groupId>
        <artifactId>embedmongo-spring</artifactId>
        <version>RELEASE</version>
        <exclusions>
            <exclusion>
                <artifactId>de.flapdoodle.embed.process</artifactId>
                <groupId>de.flapdoodle.embed</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>
        <scope>test</scope>
    </dependency>