javamongodbspring-bootfongomongobee

Error creating mongobee bean on springboot app (on run of tests)


I use all lasts versions (fix others problems)

mongobee 0.13

fongo 2.2.0-RC3-SNAPSHOT

mongodb-driver 3.8.2

When I run my app, I have this error:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongobeeStandalone' defined in class path resource [com/myproject/company/configuration/MongoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.AbstractMethodError: com.mongodb.client.internal.FongoOperationExecutor.execute(Lcom/mongodb/operation/ReadOperation;Lcom/mongodb/ReadPreference;Lcom/mongodb/ReadConcern;)Ljava/lang/Object; ... Caused by: java.lang.AbstractMethodError: com.mongodb.client.internal.FongoOperationExecutor.execute(Lcom/mongodb/operation/ReadOperation;Lcom/mongodb/ReadPreference;Lcom/mongodb/ReadConcern;)Ljava/lang/Object;

My class:

@Bean
public Mongobee mongobeeStandalone(final MongoTemplate mongoTemplate, final MongoClient mongoClient) {
    final Mongobee runner = new Mongobee(mongoClient);
    runner.setChangeLogsScanPackage(Changelog1.class.getPackage().getName()); // package to scan for changesets
    runner.setDbName(mongoTemplate.getDb().getName());
    return runner;
}

@Bean
public MongoClient mongoClient(final MongoDbFactory mongoDbFactory) {
    final Mongo mongo = mongoDbFactory.getLegacyDb().getMongo();
    if (!MongoClient.class.isInstance(mongo)) {
        throw new UnsupportedOperationException("Must be a MongoClient");
    }
    return MongoClient.class.cast(mongo);
}

EDIT

My test configuration:

@Configuration
public class FoncgoConfiguration extends AbstractMongoConfiguration {

    @Bean
    public Fongo fongo() {
        return new Fongo("mongo-test");
    }

    @Override
    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        final SimpleMongoDbFactory simpleMongoDbFactory = new SimpleMongoDbFactory(mongoClient(), getDatabaseName());
        final MongoTemplate mongoTemplate = new MongoTemplate(simpleMongoDbFactory, mappingMongoConverter());
        return mongoTemplate;
    }

    @Override
    @Bean
    public MongoClient mongoClient() {
        return MockMongoClient.create(fongo());
    }

}

Solution

  • You should use Spring Data Mongo, it will create and inject com.mongodb.MongoClient bean. So remove mongoClient() method and add the Mongodb Spring Data dependency:

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

    Spring Boot also provides a MongoTemplate bean and auto-configures it. For testing you should use Embedded Mongo with @DataMongoTest annotation, because it configures a MongoTemplate. See here for test examples.