flapdoodle-oss / de.flapdoodle.embed.mongo.spring

embedded mongo spring integration
Apache License 2.0
30 stars 7 forks source link

java.lang.NullPointerException in mongo repository - Spring boot 2.7.5 #7

Closed RaghuManickam closed 1 year ago

RaghuManickam commented 1 year ago

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

@SpringBootTest
@AutoConfigureDataMongo
@DirtiesContext
@EnableAutoConfiguration
@TestPropertySource(properties = "property=C")
public class OperatorRepositoryTest {

private Operator operator1;

@Autowired
    private OperatorRepository operatorRepository;

public void createNewInstance() {
        this.operator1 = new Operator();
        operatorList = new ArrayList<>();
    }

    @Before
    public void init() throws Exception {
        createNewInstance();
        operator1.setdata("test");
        operatorList.add(operator1);
        **operatorRepository**.saveAll(operatorList);

    }
}

pom.xml

<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo.spring27x</artifactId>
<version>4.3.2</version>
</dependency>
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>4.2.0</version>
</dependency>

Application.properties

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.mongo.embedded.EmbeddedMongoAutoConfiguration
de.flapdoodle.mongodb.embedded.version=6.0.1
RaghuManickam commented 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)

michaelmosmann commented 1 year ago

.. will look into this next week.

RaghuManickam commented 1 year ago

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

}`

RaghuManickam commented 1 year ago

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

michaelmosmann commented 1 year ago

@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