fakemongo / fongo

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

Support for java mongodb driver 3.6.4+ #357

Open albertotn opened 5 years ago

albertotn commented 5 years ago

I've tried to use Fongo with java mongodb driver 3.7.1 and receive this exception (with Java 8):

java.lang.NoClassDefFoundError: com/mongodb/OperationExecutor
    at com.github.fakemongo.Fongo.createMongo(Fongo.java:190)
    at com.github.fakemongo.Fongo.<init>(Fongo.java:103)
    at com.github.fakemongo.Fongo.<init>(Fongo.java:90)
    at com.github.fakemongo.Fongo.<init>(Fongo.java:82)
    at it.gpi.flowmanager2.batch.SingleValueHandlerTest.setup(SingleValueHandlerTest.java:39)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
    at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:538)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:760)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:460)
    at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:206)
Caused by: java.lang.ClassNotFoundException: com.mongodb.OperationExecutor
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:338)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 20 more

Reading 3.7.x release notes: http://mongodb.github.io/mongo-java-driver/3.7/whats-new/ seems to be a problem related to new Java 9 modular approach.

I've found branch driver37 but I'm not sure If I need to use it or stay with master, please let me know

ngbalk commented 5 years ago

I'm using mongo-java-driver 3.8.2 and getting the exact same exception (also with Java 8).

markbigler commented 5 years ago

Same issue for me with mongodb-driver 3.8.2 (spring boot 2.1.0).

The class com.mongodb.OperationExecutor as used in MockMongoClient is no longer available.

 com.mongodb.OperationExecutor createOperationExecutor() {
   return new FongoOperationExecutor(fongo);
 }
Writtscher commented 5 years ago

@twillouer Hi, can we have a response regarding this issue? What is the priority of this issue as it is currently a show stopper for our project, as we cannot upgrade to latest the spring/mongo version.

I would like to avoid switching to another (embedded) mongo solution...

tschiman commented 5 years ago

We are also having a similar problem. Using driver 3.8.2 with spring boot 2.1.0. We are getting a null pointer exception in the com.mongodb.Mongo class when attempting to access the:

private final MongoClientDelegate delegate;

This element is not set by the MockMongoClient from Fongo and certain operations fail. The operation we are executing is:

mongoTemplate.indexOps(QueueEntry.class).ensureIndex(new Index(CREATED_DATE_FIELD_NAME, Sort.Direction.DESC));

Here is the stack trace from the failure:

java.lang.NullPointerException at com.mongodb.Mongo.createOperationExecutor(Mongo.java:790) at com.mongodb.DB.(DB.java:101) at com.mongodb.FongoDB.(FongoDB.java:43) at com.github.fakemongo.Fongo.getDB(Fongo.java:117) at com.mongodb.FongoMongoCollection.(FongoMongoCollection.java:28) at com.mongodb.FongoMongoDatabase.getCollection(FongoMongoDatabase.java:47) at org.springframework.data.mongodb.core.MongoTemplate.getAndPrepareCollection(MongoTemplate.java:2574) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:534) at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:521) at org.springframework.data.mongodb.core.DefaultIndexOperations.execute(DefaultIndexOperations.java:215) at org.springframework.data.mongodb.core.DefaultIndexOperations.ensureIndex(DefaultIndexOperations.java:121)

markbigler commented 5 years ago

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

Except for 2 test cases the switch from fongo to mongo-java-server went without any problems for me. I reported them and they were fixed very quickly. Thanks @bwaldvogel

jaypatel512 commented 5 years ago

Same issue as @tschiman !

jaypatel512 commented 5 years ago

Btw @tschiman ! Seeing that it was created on Sep 20, and nothing has changed in last 4 months, I switched to mongo-java-server, as @markbigler suggested, and it worked like a charm. No code change required in my case. Just a small change in my test-mongo.xml spring configurations, and everything went smoothly.

I would highly recommend doing that to anyone who comes here, until the bug is fixed.

medialwerk commented 5 years ago

I invested three days before my holidays fixing my specs and didn't get it done. With @markbigler solution, all tests were green again within 10 minutes 💯. Big thanks! :-)

Writtscher commented 5 years ago

We switched one week later and it everything is working. We will not switch back anymore.

ndemengel commented 5 years ago

Hi everyone,

We (at Malt) encountered the same problems as everyone:

So I finally dedicated some hours to fongo itself and here is the result: fongo-mongo-3.8. Please note that only the minimum amount of work required to have all tests pass with the 3.8 driver has been done. I couldn't find a way to contribute it in an "intelligent" way, since it can't just be a pull request for a new version of fongo (it requires Java 8+ and mongo 3.8), so I called it fongo-mongo-3.8 and made it clear that it's not provided by the fongo team (for now). Also we didn't publish it to Maven central, so you will have to build it by yourself if you want to use it. I hope it can be reintegrated into the fongo project in some way, but meanwhile we at least have a way to move forward.

As a side note, I'd like to share some thoughts if it can be of any help to someone else: we do think in my team that though fongo is a nice tool, we should not rely that much on it. We are improving our testing strategy to mock our repository interfaces (which is easier now that we're more and more applying the hexagonal architecture principles), and we test our repository implementations using a real database via https://www.testcontainers.org/. Also, we're in the process of dropping MongoDB, but that's another story :-)

rashmidash commented 4 years ago

Btw @tschiman ! Seeing that it was created on Sep 20, and nothing has changed in last 4 months, I switched to mongo-java-server, as @markbigler suggested, and it worked like a charm. No code change required in my case. Just a small change in my test-mongo.xml spring configurations, and everything went smoothly.

I would highly recommend doing that to anyone who comes here, until the bug is fixed.

Hi @jaypatel512,

We are trying to fix using same type of xml configuration. Could you please share your test-mongo.xml or inbox me the same. (rashmidash2016@yahoo.com)

markbigler commented 4 years ago

We are trying to fix using same type of xml configuration. Could you please share your test-mongo.xml or inbox me the same. (rashmidash2016@yahoo.com)

Solution: https://github.com/bwaldvogel/mongo-java-server/issues/103#issuecomment-542664081