Closed RaghuManickam closed 1 year ago
Hi ,
Sorry My Bad It was due to class path issue . fixed it added.
Config
`@ComponentScan(basePackages = {"com.sample.code"}) @Configuration @EnableAutoConfiguration( exclude = {DataSourceAutoConfiguration.class}) public class Test1 implements InitializingBean, DisposableBean {
private MongodExecutable executable;
private static final String DB_NAME = "data_synchronize";
@Bean
public MongoDatabaseFactory mongoDatabaseFactory(){
String connectionString = "mongodb://localhost:27019/"+DB_NAME;
return new SimpleMongoClientDatabaseFactory(connectionString);
}
@Bean
public MongoTemplate mongoTemplate(MongoDatabaseFactory mongoDatabaseFactory){
MongoTemplate mongoTemplate = new MongoTemplate(mongoDatabaseFactory);
mongoTemplate.setWriteConcern(WriteConcern.ACKNOWLEDGED);
return mongoTemplate;
}
@Override
public void destroy() throws Exception {
executable.stop();
}
@Override
public void afterPropertiesSet() throws Exception {
MongodConfig mongoConfig = MongodConfig.builder()
.version(Version.V5_0_2)
.net(new Net("localhost", 27019, Network.localhostIsIPv6()))
.build();
MongodStarter starter = MongodStarter.getDefaultInstance();
executable = starter.prepare(mongoConfig);
executable.start();
}
}`
@RunWith(SpringRunner.class) @SpringBootTest(classes = Test1.class) public class OperatorRepositoryTest {}
but now I am facing another issue with version
org.springframework.dao.OptimisticLockingFailureException: Cannot save entity 63a54bc6fc7a1868ba56612a with version 1 to collection pls_operator. Has it been modified meanwhile? at org.springframework.data.mongodb.core.MongoTemplate.doSaveVersioned(MongoTemplate.java:1509) at org.springframework.data.mongodb.core.MongoTemplate.save(MongoTemplate.java:1472)
.. will look into this next week.
adding to the above point I have created the test cases in different module .
I thinks this make the problem .
error without custom class
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
as I have created custom config class .
`@ComponentScan(basePackages = {"com.x.sample"}) @Configuration @DataMongoTest( excludeAutoConfiguration = org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration.class ) @EnableAutoConfiguration(exclude = {DataSourceAutoConfiguration.class}) @TestPropertySource(properties = "property=A") @ExtendWith(SpringExtension.class) @DirtiesContext public class Test2 implements InitializingBean, DisposableBean { private static final String MONGO_URI = "mongodb://%s:%s@%s";
private static final String DB_NAME = "data_synchronize";
private MongodExecutable executable;
@Bean
public MongoClient mongoClient() {
MongoClientSettings mongoClientSettings = MongoClientSettings.builder()
.writeConcern(WriteConcern.ACKNOWLEDGED)
.applyConnectionString(new ConnectionString(MONGO_URI))
.build();
return MongoClients.create(mongoClientSettings);
}
@Bean
public MongoTemplate mongoTemplate() {
return new MongoTemplate(mongoClient(), DB_NAME);
}
@Override
public void destroy() throws Exception {
executable.stop();
}
@Override
public void afterPropertiesSet() throws Exception {
MongodConfig mongoConfig = MongodConfig.builder()
.version(Version.V5_0_2)
.net(new Net("localhost", 27019, Network.localhostIsIPv6()))
.build();
MongodStarter starter = MongodStarter.getDefaultInstance();
executable = starter.prepare(mongoConfig);
executable.start();
}
}`
Sorry It was a class path issue . where I was using multiple modules and spring application and mongo test case are in different module . so closing this issue .
But still i have issue with mongo config which makes hard to fix the issue
@RaghuManickam .. just create an issue for that.. but as i am busy right now i will have a look into this not before new year
Hi team ,
Please guide me through what am I missing . And I am also using @version in my entity please let me know additional config need to be added .like WriteConcern.ACKNOWLEDGED
Test Class
pom.xml
Application.properties