Reputation: 3549
I am trying to upgrade mongodb to 4.x from 3.x since we need to have transaction support in mongodb, and also using fongo for junits. I have modified version for below given dependencies :
<spring-data-mongodb-version>2.1.0.RELEASE</spring-data-mongodb-version>
<spring-data-commons-version>2.1.0.RELEASE</spring-data-commons-version>
<spring-framework-version>5.0.8.RELEASE</spring-framework-version>
<mongo-java-driver>3.2.8</mongo-java-driver>
fongo -> 2.1.0
Added below given code toTestConfig class:
private static final MongoClient createNewMongo() {
Fongo fongo = new Fongo("fongo");
MongoClient mongo = fongo.getMongo();
return mongo;
}
@Override
@Bean
public MongoTemplate mongoTemplate() throws Exception {
Fongo fongo = new Fongo("fongo");
MongoClient mongo = fongo.getMongo();
return new MongoTemplate(mongo, DATABASE_NAME);
}
@Bean
public MongoClient mongoClient() {
MongoCredential mongoCredential = MongoCredential
.createMongoCRCredential("ext-catalog", "ext-catalog",
"ext-catalog".toCharArray());
List<MongoCredential> credentialsList = new ArrayList<>();
credentialsList.add(mongoCredential);
ServerAddress serverAddress = null;
try {
serverAddress = new ServerAddress("localhost" , 27017 );
} catch (Exception e) {
e.printStackTrace();
}
return new MongoClient(serverAddress, credentialsList);
}
Getting below given error :
java.lang.NoClassDefFoundError: com/mongodb/FongoMongoDatabase
at com.mongodb.MockMongoClient.getDatabase(MockMongoClient.java:88) ~[fongo-2.1.0.jar:na]
at org.springframework.data.mongodb.core.SimpleMongoDbFactory.doGetMongoDatabase(SimpleMongoDbFactory.java:105) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.MongoDbFactorySupport.getDb(MongoDbFactorySupport.java:107) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.MongoDbFactorySupport.getDb(MongoDbFactorySupport.java:95) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.MongoDatabaseUtils.doGetMongoDatabase(MongoDatabaseUtils.java:108) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.MongoDatabaseUtils.getDatabase(MongoDatabaseUtils.java:68) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.doGetDatabase(MongoTemplate.java:2235) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.MongoTemplate.execute(MongoTemplate.java:534) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.DefaultIndexOperations.execute(DefaultIndexOperations.java:218) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.DefaultIndexOperations.ensureIndex(DefaultIndexOperations.java:121) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
at org.springframework.data.mongodb.core.index.MongoPersistentEntityIndexCreator.createIndex(MongoPersistentEntityIndexCreator.java:145) ~[spring-data-mongodb-2.1.0.RELEASE.jar:2.1.0.RELEASE]
Is there any way to fix it ? I found a link which say these 2 are not compatible.
Upvotes: 1
Views: 2024
Reputation: 6960
I had the same problem, it was a matter of finding the correct match between fongo version and mongo version. Since it is annoying to have this sort of errors and also because it may help others in the future:
in this example, you can see that fongo 2.1.0 matches mongo 3.4.2.
Upvotes: 0
Reputation: 1216
First of all, 3.2.8 seems very old if you are using Spring Data 2.1.X. I would stick to whatever version spring-data-mongodb depends on. Even then, Fongo is not compatible with the newer mongo drivers. There are several open issues related to this: #316, #337, #357
The last issue above links to a fork of Fongo that might work for you. Or you could try to downgrade the mongo driver a bit to 3.7 and use fongo 2.2.0-RC1, which also supposedly works according to the same issue. I'm not sure if that would be compatible with your Spring Data version, however.
Upvotes: 1
Reputation: 383
I have an application that runs in JDK 11 and Spring 2.1 and also uses Fongo for mocking Mongo response.
My application is working fine under the below combination of packages
Use com.github.fakemongo:fongo 2.2.0-RC1 (exception is found when using 2.2.0-RC2)
spring-boot-dependencies 2.1.x (as Spring Boot 2.1 brings Java 11 support)
You need to override some of the dependencies in Spring 2.1 as below
More importantly, after defining the above dependencies in your pom.xml run "clean dependency:tree" to see if your application OR modules picks up the correct packages.
[INFO] +- org.springframework.boot:spring-boot-starter-data-mongodb:jar:2.0.8.RELEASE:compile
[INFO] | +- org.mongodb:mongodb-driver:jar:3.6.4:compile
[INFO] | | +- org.mongodb:bson:jar:3.6.4:compile
[INFO] | | \- org.mongodb:mongodb-driver-core:jar:3.6.4:compile
Upvotes: 1
Reputation: 2897
I think at the moment the best solution is switching to [mongo java server][1].
That´s how I got the tests working with the latest versions.
Upvotes: 1