Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Could not find class [de.flapdoodle.embed.process.config.IRuntimeConfig]

After upgrading to embeded MongoDB with version 3.0.0, I am getting the following exception.

I am using the following in build.gradle.

testImplementation group: 'de.flapdoodle.embed', name: 'de.flapdoodle.embed.mongo', version: '3.0.0'

The exception is given below.

Caused by: java.lang.ClassNotFoundException: de.flapdoodle.embed.process.config.IRuntimeConfig
    at java.net.URLClassLoader.findClass(URLClassLoader.java:382) ~[na:1.8.0_261]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_261]
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:355) ~[na:1.8.0_261]
    at java.lang.ClassLoader.loadClass(ClassLoader.java:351) ~[na:1.8.0_261]
    at java.lang.Class.forName0(Native Method) ~[na:1.8.0_261]
    at java.lang.Class.forName(Class.java:348) ~[na:1.8.0_261]
    at org.springframework.util.ClassUtils.forName(ClassUtils.java:284) ~[spring-core-5.2.10.RELEASE.jar:5.2.10.RELEASE]
    at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:324) ~[spring-core-5.2.10.RELEASE.jar:5.2.10.RELEASE]

I provide below the code. I want to use embeded mongoDB for the MongoDB transaction feature in the integration test in spring boot.

@Profile("test")
@ActiveProfiles("test")
public class TestMongoConfig1 implements InitializingBean, DisposableBean {

    MongodForTestsFactory factory = null;
    
    MongodConfig mongodConfig = MongodConfig.builder().version(Version.Main.PRODUCTION).build();

    MongodStarter runtime = MongodStarter.getDefaultInstance();

    MongodExecutable mongodExecutable = null;
    MongodProcess mongod = null;
    
    @Override
    public void destroy() throws Exception {
        mongodExecutable.stop();
        
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        mongodExecutable = runtime.prepare(mongodConfig);
        mongod = mongodExecutable.start();
    }
    
    @Bean(name = "test1")
    public MongoClient mongoClient() {
        MongoClient mongoClient = MongoClients.create();
        System.out.println("============================================");
        System.out.println(mongoClient);
        System.out.println("============================================");
        
        return mongoClient;
    }

}
like image 929
PythonLearner Avatar asked Feb 26 '21 07:02

PythonLearner


Video Answer


1 Answers

First you have to use the following dependencies in build.gradle apart from other dependencies if you are using @DataMongoTest.

implementation("org.springframework.boot:spring-boot-starter-data-mongodb")
testImplementation("de.flapdoodle.embed:de.flapdoodle.embed.mongo")

To test with embeded MongoDB, you have to use the following snippet which may work.

@Profile("test")
@ActiveProfiles("test")
@Configuration
public class TestMongoConfig1 implements InitializingBean, DisposableBean {

    private MongodExecutable executable;

    
    @Override
    public void afterPropertiesSet() throws Exception {

        IFeatureAwareVersion version = Versions.withFeatures(new GenericVersion("4.0.0"), 
         Version.Main.PRODUCTION.getFeatures());
        IMongoCmdOptions cmdOptions = new 
         MongoCmdOptionsBuilder().useNoPrealloc(false).useSmallFiles(false)
                .master(false).verbose(false).useNoJournal(false).syncDelay(0).build();
        int port = Network.getFreeServerPort();
        IMongodConfig mongodConfig = new MongodConfigBuilder().version(version)
                .net(new Net(port, Network.localhostIsIPv6())).replication(new Storage(null, "testRepSet", 5000))
                .configServer(false).cmdOptions(cmdOptions).build();
        MongodStarter starter = MongodStarter.getDefaultInstance();
        executable = starter.prepare(mongodConfig);
        executable.start();
    }
like image 133
Sambit Avatar answered Oct 19 '22 22:10

Sambit