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

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

How to use it with Springboot 3.0.X ? #13

Closed cosad3s closed 1 year ago

cosad3s commented 1 year ago

Hello,

I need help.

I have a standard repository:

@Repository
public interface MyRepository extends MongoRepository<MyData, String> {
}

Previously, with my app on Spring 2.x, it was working fine with:

@DataMongoTest
public class MyRepositoryTest {

    @Autowired
    MyRepository myRepo;

    @Test
    public void ShouldDoThis() {
        ...
    }
}

But I have this error: org.springframework.dao.DataAccessResourceFailureException: Timed out after 30000 ms while waiting to connect. Client view of cluster state is {type=UNKNOWN, servers=[{address=localhost:27017, type=UNKNOWN, state=CONNECTING, exception={com.mongodb.MongoSocketOpenException: Exception opening socket}, caused by {java.net.ConnectException: Connexion refusée}}]

I tried to change to:

@ActiveProfiles("test")
@SpringBootApplication
@SpringBootTest
@TestPropertySource(properties = {
        "de.flapdoodle.mongodb.embedded.version=4.2.23"
        ,"spring.data.mongodb.uri=mongodb://localhost/test"
})
public class MyRepositoryTest {
// ...

But always the same error.

The documentation is not very clear to me (In Spring Boot 3 too) on how to test Mongo Repository with embedded DB ...

Thanks in advance

michaelmosmann commented 1 year ago

@cosad3s which dependencies do you have? which version of this library?

cosad3s commented 1 year ago

Thanks for your answer. Sorry for missing details, I am trying to use:

<dependency>
    <groupId>de.flapdoodle.embed</groupId>
    <artifactId>de.flapdoodle.embed.mongo.spring30x</artifactId>
    <version>4.4.1</version>
</dependency>

With:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>3.0.2</version>
        <relativePath/>
    </parent>

And:

❯ java -version
Picked up _JAVA_OPTIONS: -Dawt.useSystemAAFontSettings=on -Dswing.aatext=true
openjdk version "17.0.5" 2022-10-18
michaelmosmann commented 1 year ago

@cosad3s i thing you need at least @DataMongoTest .. just add this again see if this works

cosad3s commented 1 year ago

With:

@ActiveProfiles("test")
@SpringBootApplication
@TestPropertySource(properties = {
        "de.flapdoodle.mongodb.embedded.version=4.2.23"
        ,"spring.data.mongodb.uri=mongodb://localhost/test"
})
@DataMongoTest
public class MyRepositoryTest {
...

I removed @SpringBootTest because an error happens about a conflict.

... and same error with local test launch.

I noticed I get a stacktrace when I launch globally the tests (i.e. all tests of the project):

Caused by: java.lang.IllegalStateException: Set the de.flapdoodle.mongodb.embedded.version property or define your own IFeatureAwareVersion bean to use embedded MongoDB
    at org.springframework.util.Assert.state(Assert.java:76)
    at de.flapdoodle.embed.mongo.spring.autoconfigure.EmbeddedMongoAutoConfiguration.determineVersion(EmbeddedMongoAutoConfiguration.java:120)
    at de.flapdoodle.embed.mongo.spring.autoconfigure.EmbeddedMongoAutoConfiguration.version(EmbeddedMongoAutoConfiguration.java:116)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:139)
    ... 174 common frames omitted

I tried to define the version in static way instead in the class annotations, in the MyRepositoryTest class:

    static {
        System.setProperty("de.flapdoodle.mongodb.embedded.version","4.2.23");
    }

I tried another versions (5.0.5 ...). No change.

I think I will migrate to TestContainer unfortunately and ... temporary ? Not really fan of that because I give some privileges to my docker daemon.

michaelmosmann commented 1 year ago

@cosad3s as these property stuff is all this spring magic i am not sure if a system property is used for this.. did you try these spring config files? as here: https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.spring/blob/main/src/test/resources/application.properties

michaelmosmann commented 1 year ago

@cosad3s can you try if this projects works https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo.canary .. this is a canary project to see if everything works as expected.

cosad3s commented 1 year ago

Thanks @michaelmosmann , I'll check this out later to do some debug

michaelmosmann commented 1 year ago

@michaelmosmann i close this issue - reopen if needed:)

cosad3s commented 1 year ago

Sure, thank you, I'll return to this problem maybe later ...

cosad3s commented 1 year ago

Ok, I have checked again my project by comparing with your suggestion.

After some tests, it appears that the problem came from the IDE cache. With IntelliJ, after doing "File > Invalidate Caches ..." it worked fine after. :)

MyRepositoryTest.java

@DataMongoTest
@ExtendWith(SpringExtension.class)
public class MyRepositoryTest {

@Autowired
    MyRepository myRepository;

    @BeforeEach
    void setUp() {
        // Prepare fake data in DB
        myRepository.saveAll(entities);
    }

    @AfterEach
    void tearDown() {
        myRepository.deleteAll();
    }

    // Example
    @Test
    void createData() {
        myRepository.save(d);
        var data = myRepository.findAll();
        Assertions.assertEquals(4, data.size());
        Assertions.assertTrue(data.contains(d));
    }
}

application.properties (for tests)

de.flapdoodle.mongodb.embedded.version=5.0.5

But something I do not understand correctly ... What are the valid version for de.flapdoodle.mongodb.embedded.version ? For example, 5.0.5 is valid but not listed in https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/tags. 4.6.2 is invalid but listed. I got an error:

Caused by: java.lang.IllegalArgumentException: linux distribution not supported: GenericFeatureAwareVersion{4.6.2}:Platform{operatingSystem=Linux, architecture=X86_64}(with fallback to GenericFeatureAwareVersion{4.6.2}:Platform{operatingSystem=Linux, architecture=X86_64, version=Ubuntu_20_04})
michaelmosmann commented 1 year ago

@cosad3s de.flapdoodle.mongodb.embedded.version is related to the mongodb version found here: https://www.mongodb.com/try/download/community .. my project is just a wrapper, the version of the wrapper does not relate in any way to the mongodb server version you want to start.. :)