0

I'm using SpringBoot 2.6.2 together with flapdoodle 3.2.3 for test. If I just want to test DB stuff, then @DataMongoTest works finde, because for tests I want to test it with an in-memory database. If I want to test RestConnections I'm using @SpringBootTest and also everything is fine.

Now I just want to use both, I need to test to send something to an rest connection and at the method on the other side it needs to check the database if the data is already there or not.

My problem now is, that I cannot use both @DataMongoTest and @SpringBootTest, they are not combinable, but I still need the autowired of all my Services, Components, etc. why I need SpringBootTest and also I need my embedded database, because I don't want to prerequisite an installed mongoDB with service.

I already tried @AutoConfigureDataMongo together with @SpringBootTest, like it is mentioned here: enter link description here but it will not work.

Any suggestions how I can combine both?

Thanks and Best, Lobo

PS: Here is my pom provide which libraries I'm using in which version

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    
    <properties>
        <java.version>17</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>com.github.openjson</groupId>
            <artifactId>openjson</artifactId>
            <version>1.0.11</version>
        </dependency>
        <dependency>
            <groupId>com.github.erosb</groupId>
            <artifactId>everit-json-schema</artifactId>
            <version>1.14.0</version>
        </dependency>
        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.mongo</artifactId>
            <version>3.2.3</version><!--$NO-MVN-MAN-VER$-->
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>de.flapdoodle.embed</groupId>
            <artifactId>de.flapdoodle.embed.process</artifactId>
            <version>3.1.4</version>
            <scope>test</scope>
        </dependency>
    </dependencies>
Lobo
  • 73
  • 1
  • 1
  • 5

1 Answers1

0

OK I created just my own database in a @beforeAll like it is described here:

https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo

MongodStarter starter = MongodStarter.getDefaultInstance();

int port = Network.getFreeServerPort();
MongodConfig mongodConfig = MongodConfig.builder()
    .version(Version.Main.PRODUCTION)
    .net(new Net(port, Network.localhostIsIPv6()))
    .build();

MongodExecutable mongodExecutable = null;
try {
  mongodExecutable = starter.prepare(mongodConfig);
  MongodProcess mongod = mongodExecutable.start();

  try (MongoClient mongo = new MongoClient("localhost", port)) {
    DB db = mongo.getDB("test");
    DBCollection col = db.createCollection("testCol", new BasicDBObject());
    col.save(new BasicDBObject("testDoc", new Date()));
  }

} finally {
  if (mongodExecutable != null)
    mongodExecutable.stop();
}

with this I can use @SpringBootTest and still have the connection to the embedded database.

Lobo
  • 73
  • 1
  • 1
  • 5