0

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());
    }

}
Stéphane GRILLON
  • 11,140
  • 10
  • 85
  • 154
  • I'm not able to find the class `com.mongodb.client.internal.FongoOperationExecutor`. Do you have a link to it's API? I found the same class from a different project [here](https://jar-download.com/artifacts/com.github.fakemongo/fongo/2.2.0-RC1/source-code/com/mongodb/FongoOperationExecutor.java). – Boris Oct 18 '18 at 08:47
  • source link: https://github.com/fakemongo/fongo/blob/driver37/src/main/java/com/mongodb/client/internal/FongoOperationExecutor.java – Stéphane GRILLON Oct 18 '18 at 09:23
  • How is the `MongoClient` being created? – Boris Oct 18 '18 at 09:31
  • I edit my question with my @Bean mongoClient – Stéphane GRILLON Oct 18 '18 at 09:37
  • When does the error happen, during the run or testing? This will narrow it down to a corresponding configuration. – Boris Oct 18 '18 at 10:26

1 Answers1

0

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.

Boris
  • 22,667
  • 16
  • 50
  • 71
  • I have this dependency but I have an other conf in my tests class (mongoClient, mongoTemplate, ...) – Stéphane GRILLON Oct 18 '18 at 10:05
  • with this (@DataMongoTest) you do not use Fongo? – Stéphane GRILLON Oct 18 '18 at 10:30
  • It depends on what are you testing, also whether it's a unit test or an integration test. Maybe you should share your test(s) as well? – Boris Oct 18 '18 at 10:56
  • Oh, I see what you mean. Correct you should be using embedded MongoDB instead. Spring Boot offers auto-configuration for it, see [here](https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-mongo-embedded) for more details. – Boris Oct 18 '18 at 11:19