diff --git a/spring-boot/.factorypath b/spring-boot/.factorypath index 68b2514aab..01b84b761a 100644 --- a/spring-boot/.factorypath +++ b/spring-boot/.factorypath @@ -1,4 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + @@ -16,7 +38,6 @@ - @@ -32,9 +53,7 @@ - - @@ -48,7 +67,6 @@ - @@ -85,6 +103,9 @@ + + + @@ -136,7 +157,6 @@ - diff --git a/spring-boot/pom.xml b/spring-boot/pom.xml index d8ee3cc2d9..70c3bb4c6b 100644 --- a/spring-boot/pom.xml +++ b/spring-boot/pom.xml @@ -1,5 +1,5 @@ + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 com.baeldung spring-boot @@ -16,6 +16,35 @@ + + + org.springframework.boot + spring-boot-starter-data-mongodb + + + de.flapdoodle.embed + de.flapdoodle.embed.mongo + test + + + + org.junit.jupiter + junit-jupiter-api + test + + + org.junit.jupiter + junit-jupiter-engine + test + + + + + org.junit.platform + junit-platform-launcher + ${junit-platform.version} + test + org.springframework.boot spring-boot-starter-thymeleaf @@ -55,11 +84,6 @@ test - - io.dropwizard.metrics metrics-core @@ -170,7 +194,7 @@ **/*LiveTest.java **/*IntegrationTest.java - **/*IntTest.java + **/*IntTest.java **/AutoconfigurationTest.java diff --git a/spring-boot/src/main/java/com/baeldung/mongodb/Application.java b/spring-boot/src/main/java/com/baeldung/mongodb/Application.java new file mode 100644 index 0000000000..092ce3352b --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/mongodb/Application.java @@ -0,0 +1,11 @@ +package com.baeldung.mongodb; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class Application { + public static void main(String[] args) { + SpringApplication.run(Application.class, args); + } +} diff --git a/spring-boot/src/test/java/com/baeldung/mongodb/ManualEmbeddedMongoDbIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/mongodb/ManualEmbeddedMongoDbIntegrationTest.java new file mode 100644 index 0000000000..30a4d61fbd --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/mongodb/ManualEmbeddedMongoDbIntegrationTest.java @@ -0,0 +1,62 @@ +package com.baeldung.mongodb; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.data.mongodb.core.MongoTemplate; + +import com.mongodb.BasicDBObjectBuilder; +import com.mongodb.DBObject; +import com.mongodb.MongoClient; + +import de.flapdoodle.embed.mongo.MongodExecutable; +import de.flapdoodle.embed.mongo.MongodStarter; +import de.flapdoodle.embed.mongo.config.IMongodConfig; +import de.flapdoodle.embed.mongo.config.MongodConfigBuilder; +import de.flapdoodle.embed.mongo.config.Net; +import de.flapdoodle.embed.mongo.distribution.Version; +import de.flapdoodle.embed.process.runtime.Network; + +class ManualEmbeddedMongoDbIntegrationTest { + private MongodExecutable mongodExecutable; + private MongoTemplate mongoTemplate; + + @AfterEach + void clean() { + mongodExecutable.stop(); + } + + @BeforeEach + void setup() throws Exception { + String ip = "localhost"; + int port = 27017; + + IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION) + .net(new Net(ip, port, Network.localhostIsIPv6())) + .build(); + + MongodStarter starter = MongodStarter.getDefaultInstance(); + mongodExecutable = starter.prepare(mongodConfig); + mongodExecutable.start(); + mongoTemplate = new MongoTemplate(new MongoClient(ip, port), "test"); + } + + @DisplayName("Given object When save object using MongoDB template Then object can be found") + @Test + void test() throws Exception { + // given + DBObject objectToSave = BasicDBObjectBuilder.start() + .add("key", "value") + .get(); + + // when + mongoTemplate.save(objectToSave, "collection"); + + // then + assertThat(mongoTemplate.findAll(DBObject.class, "collection")).extracting("key") + .containsOnly("value"); + } +} diff --git a/spring-boot/src/test/java/com/baeldung/mongodb/MongoDbSpringIntegrationTest.java b/spring-boot/src/test/java/com/baeldung/mongodb/MongoDbSpringIntegrationTest.java new file mode 100644 index 0000000000..5431217c3e --- /dev/null +++ b/spring-boot/src/test/java/com/baeldung/mongodb/MongoDbSpringIntegrationTest.java @@ -0,0 +1,36 @@ +package com.baeldung.mongodb; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.data.mongo.DataMongoTest; +import org.springframework.data.mongodb.core.MongoTemplate; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit.jupiter.SpringExtension; + +import com.mongodb.BasicDBObjectBuilder; +import com.mongodb.DBObject; + +@ContextConfiguration(classes = Application.class) +@DataMongoTest +@ExtendWith(SpringExtension.class) +public class MongoDbSpringIntegrationTest { + @DisplayName("Given object When save object using MongoDB template Then object can be found") + @Test + public void test(@Autowired MongoTemplate mongoTemplate) { + // given + DBObject objectToSave = BasicDBObjectBuilder.start() + .add("key", "value") + .get(); + + // when + mongoTemplate.save(objectToSave, "collection"); + + // then + assertThat(mongoTemplate.findAll(DBObject.class, "collection")).extracting("key") + .containsOnly("value"); + } +}