fakemongo / fongo

faked out in-memory mongo for java
Apache License 2.0
523 stars 156 forks source link

fongo not compatible with 3.7 driver version #337

Open zigzago opened 6 years ago

zigzago commented 6 years ago

MongoOperation, MongoDatabaseImpl, MongoCollectionImpl, etc. have moved to com.mongodb.client.internal package...

twillouer commented 6 years ago

can you try with driver37 branch ?

zigzago commented 6 years ago

my tests pass with driver37 branch, thanks! It would be nice to have a first fongo release for 3.7 :)

d0x commented 6 years ago

@hoffrocket any plans about merging his PR or supporting 3.7 in general?

markbigler commented 6 years ago

There are other issues adressing the same topic: https://github.com/fakemongo/fongo/issues/357

@twillouer any news about supporting mongodb driver 3.7+?

bsautel commented 6 years ago

I am also facing this issue that prevents me from upgrading to Spring Boot 2.1.0.

I tried to test using the driver37 branch, but I could not build it with Maven. I get the following output:


-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Erreur : impossible de trouver ou charger la classe principale org.apache.maven.surefire.booter.ForkedBooter

Results :

Tests run: 0, Failures: 0, Errors: 0, Skipped: 0

[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

The error message is in french, it tells that it was impossible to find the main class org.apache.maven.surefire.booter.ForkedBooter.

I would have used the manually built jar as a workaround until the new Fongo release is published on the Maven Central repository, but this error prevents me from testing this workaround.

kiwisincebirth commented 5 years ago

Same issue for me on Spring Boot 2.1.0 mongo db driver 3.8.2 and Java 8. I am on the "fongo-2.2.0-RC2" release.

Caused by: java.lang.NoClassDefFoundError: com/mongodb/OperationExecutor at com.github.fakemongo.Fongo.createMongo(Fongo.java:190)

bsautel commented 5 years ago

I finally could build the driver37 branch thanks to the -DskipTests=true Maven option šŸ™ˆ.

I tested it with Spring Boot 2.1.0 (driver 3.8.2) and it does not work. I get this error:

java.lang.AbstractMethodError: com.mongodb.client.internal.FongoOperationExecutor.execute(Lcom/mongodb/operation/ReadOperation;Lcom/mongodb/ReadPreference;Lcom/mongodb/ReadConcern;)Ljava/lang/Object;
    at com.mongodb.DBCursor.initializeCursor(DBCursor.java:896)
    at com.mongodb.DBCursor.hasNext(DBCursor.java:148)
    at com.mongodb.DBCursor.one(DBCursor.java:697)
    at com.mongodb.DBCollection.findOne(DBCollection.java:845)
    at com.mongodb.DBCollection.findOne(DBCollection.java:805)
    at com.mongodb.DBCollection.findOne(DBCollection.java:748)
    at com.mongodb.FongoDBCollection.createIndex(FongoDBCollection.java:522)
    at com.mongodb.FongoDBCollection.<init>(FongoDBCollection.java:93)
    at com.mongodb.FongoDB.doGetCollection(FongoDB.java:96)
    at com.mongodb.FongoDB.doGetCollection(FongoDB.java:87)
    at com.mongodb.FongoDB.getCollection(FongoDB.java:82)
    at com.mongodb.client.internal.FongoMongoCollection.<init>(FongoMongoCollection.java:32)
    at com.mongodb.client.internal.FongoMongoDatabase.getCollection(FongoMongoDatabase.java:51)

It sounds like the driver 3.8 is different from the 3.7 one.

leantrace commented 5 years ago

Same here: Caused by: java.lang.NoClassDefFoundError: com/mongodb/OperationExecutor at com.github.fakemongo.Fongo.createMongo(Fongo.java:190)

Java: 11 Spring Boot: 2.1.0.RELEASE Fongo: 2.2.0-RC2 mongodb-driver: 3.8.2

bsautel commented 5 years ago

You should give a try to mongo-java-server.

I tried it recently, I encountered two blocking issues (features not yet implemented), I reported them and they were fixed very quickly. All my tests are now running with Spring Boot 2.1.0. šŸ˜€

leantrace commented 5 years ago

Thx @bsautel - works like a charm! :+1:

A short How-To for those, running into same Problem Java Runtime: 11 Syntax: Kotlin (its easy to translate to Java with IntelliJ) Spring Boot: 2.1.0.RELEASE mongo-java-server: 1.9.7 mongodb-driver: 3.8.2

Replace "fongo" with "mongo-java-server" in your pom.xml, gradle whatever

<dependency>
    <groupId>de.bwaldvogel</groupId>
    <artifactId>mongo-java-server</artifactId>
    <version>1.9.7</version>
    <scope>test</scope>
</dependency>

Add Spring Boot Configuration

@EnableAutoConfiguration(exclude = [EmbeddedMongoAutoConfiguration::class, MongoAutoConfiguration::class, MongoDataAutoConfiguration::class, SecurityAutoConfiguration::class])
@EnableMongoRepositories(basePackages = ["my.project.repositories"])
@Configuration
@ComponentScan(basePackages = ["my.project"],
        excludeFilters = [ComponentScan.Filter(classes = [SpringBootApplication::class])])
class InMemoryMongoDbConfiguration : AbstractMongoConfiguration(){

    lateinit var client: MongoClient
    lateinit var server: MongoServer

    @Bean
    fun userCascadingMongoEventListener(): CascadeSaveMongoEventListener {
        return CascadeSaveMongoEventListener()
    }

    @Throws(Exception::class)
    override fun mongoClient(): MongoClient {
        server = MongoServer(MemoryBackend())
        // bind on a random local port
        val serverAddress = server.bind()
        client = MongoClient(ServerAddress(serverAddress))
        return client
    }

    override fun getDatabaseName(): String {
        return "test"
    }

    @PreDestroy
    fun shutdown(){
        client.close()
        server.shutdown()
    }

}

Make a Test

@RunWith(SpringRunner::class)
@SpringBootTest(
  classes = [InMemoryMongoDbConfiguration::class],
  webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
class ApplicationsTest {

    @Autowired
    lateinit var testRestTemplate: TestRestTemplate

    @Test
    fun whenCalled_thenShouldReturnApplications() {
        val _prj_base = Utils.createBasicProjectFull(UUID.randomUUID().toString())
        val p = testRestTemplate.withBasicAuth("admin", "admin").postForEntity("/api/v1/projects", _prj_base, Project::class.java).body
        val req = testRestTemplate.withBasicAuth("admin", "admin").getForEntity("/api/v1/projects/${p._id}", Project::class.java)
        assertNotNull(req)
        assertEquals(HttpStatus.OK, req?.statusCode)
        Utils.assertIgnoreIdAndCreatedOn(p, req.body!!, true)
        assertEquals(setOf<Application>(), req.body!!.applications.toSet())
    }
}

Keep smiling! :)

bsautel commented 5 years ago

Nice @leantrace , thanks for sharing the replacement example!

I just noticed noticed that you don't close the server in your InMemoryMongoDbConfiguration (server.shutdown() as explained in the project documentation) when the application context is destroyed. This may lead to resource leaks in your tests execution. It's not a big deal if the tests run in a dedicated JVM that stops at the ends of tests execution but it is better to free it anyway.

bwaldvogel commented 5 years ago

@leantrace @bsautel: Iā€™m happy to hear that mongo-java-server works for you.

The Spring Boot configuration example could be something to include in the README. Do you want to submit a pull request?

ndemengel commented 5 years ago

Hi,

Not sure if I should double-post it or not, but you might be interested in that other comment of mine for a solution to have fongo working with mongo 3.8 : https://github.com/fakemongo/fongo/issues/357#issuecomment-458034953