move code to mongodb module
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
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 org.springframework.util.SocketUtils;
|
||||
|
||||
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 randomPort = SocketUtils.findAvailableTcpPort();
|
||||
|
||||
IMongodConfig mongodConfig = new MongodConfigBuilder().version(Version.Main.PRODUCTION)
|
||||
.net(new Net(ip, randomPort, Network.localhostIsIPv6()))
|
||||
.build();
|
||||
|
||||
MongodStarter starter = MongodStarter.getDefaultInstance();
|
||||
mongodExecutable = starter.prepare(mongodConfig);
|
||||
mongodExecutable.start();
|
||||
mongoTemplate = new MongoTemplate(new MongoClient(ip, randomPort), "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");
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.mongodb;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
import org.baeldung.boot.Application;
|
||||
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");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user