How to configure Replica Set with Embedded Mongo using Spring Boot and Flapdoodle for unit testing code that uses mongodb transactions

Overview

Spring Boot Embedded Mongo with Replica Set

This project defines a basic rest service that allows users to update records of a Person (name and email). It uses mongodb as database.
It also contains a Junit test for the delete person API. The test case first creates a Person in the database and calls the PersonsService's delete method to delete the person, and checks if the person was actually deleted or not.
To run the tests, flapdoodle's embedded mongo has been added as dependency with test scope.

Understanding the demo project

  1. Checkout the medium article at https://dsgaur.medium.com/how-to-enable-replica-set-in-embbedded-mongo-with-spring-boot-ddeaa079c1c8
  2. The source code should be easy to understand. Necessary comments are provided where needed.
  3. Commit messages are descriptive. Check the full commit message (not only the first line). They explain the changes in paragraph.
  4. The git commits are tagged so that you can jump to various stages of the implementation as explained in the article. The git tags are in following order:
    1. basic-service-with-tests: Basic service implementation (with Junit tests) without using mongo transactions.
    2. mongo-txn-with-failing-tests: Enabled mongo transactions in the service but now the test cases fails due to lack of replica set in embedded mongo
    3. repl-set-without-journaling: Enabled replica set using spring properties but journaling is not enabled
    4. repl-set-with-journaling: Defined custom MongodConfig to enable journaling
    5. repl-set-initiate-and-wait-for-ready: Defined methods in UnitTestConfig to initiate replica set and wait for it to get ready
    6. cleanup-unnecessary-code: Final version of the project after cleaning up unnecessary @AutoConfigureBefore and logging levels.

How to run

Run the service

  1. Run mongo container on your local by running commands
    cd docker/mongodb
    docker compose build mongo
    docker compose up -d
  2. Run following command from root project directory to generate application jar file:
    mvn clean package -DskipTests
  3. Run following command to start the application:
    java -jar target/spring-embedded-mongo-demo-1.0-SNAPSHOT.jar
  4. Import the Postman Collection (docs/postman_collection.json) into your Postman and play with it!

Run the unit tests

Run following command from root project directory:

mvn clean test

Note: Unit tests don't need running your application or the mondodb container. They are in fact "unit tests".

You might also like...

A collection of JUnit rules for testing code which uses java.lang.System.

System Rules System Rules is a collection of JUnit rules for testing code which uses java.lang.System. System Lambda is an alternative to System Rules

Dec 22, 2022

🎯 ConsoleCaptor captures console output for unit testing purposes

ConsoleCaptor Install library with: Install with maven dependency groupIdio.github.hakky54/groupId artifactIdconsolecaptor/artifactId

Dec 9, 2022

A unit testing library for varying test data.

Burst A unit testing library for varying test data. DEPRECATED: Burst remains stable and functional, but you should check out TestParameterInjector fr

Oct 9, 2022

Applied Spring Data JPA technologies including mapping, connecting real DB, Hibernate, Queries, Paging & Sorting, various Relationships, Transactions

Applied Spring Data JPA technologies including mapping, connecting real DB, Hibernate, Queries, Paging & Sorting, various Relationships, Transactions

University Management In this project, I practiced & applied Spring Data JPA technologies including mapping, connecting real DB, Hibernate, Queries, P

Sep 5, 2022

Spring Boot Rest API unit test with Junit 5, Mockito, Maven

Spring Boot Rest API unit testing with Junit 5, Mockito, Maven Apply Spring Boot @WebMvcTest for Rest Controller Unit Test with JUnit 5 and Mockito. F

Dec 22, 2022

The Spring Boot Sample App on K8S has been implemented using GKE K8S Cluster, Spring Boot, Maven, and Docker.

gke-springboot-sampleapp 👋 The Spring Boot Sample App on K8S has been implemented using GKE K8S Cluster, Spring Boot, Maven, and Docker. Usage To be

Feb 1, 2022

Spring Boot Migrator (SBM) - a tool for automated code migrations to upgrade or migrate to Spring Boot

Spring Boot Migrator (SBM) - a tool for automated code migrations to upgrade or migrate to Spring Boot

Spring Boot Migrator uses and is compatible to OpenRewrite, a powerful mass refactoring ecosystem for Java and other source code.

Jan 2, 2023

Spring Boot Login and Registration example with MySQL, JWT, Rest Api - Spring Boot Spring Security Login example

Spring Boot Login and Registration example with MySQL, JWT, Rest Api - Spring Boot Spring Security Login example

Spring Boot Login example with Spring Security, MySQL and JWT Appropriate Flow for User Login and Registration with JWT Spring Boot Rest Api Architect

Jan 5, 2023

This Web Application Allows A user to upload a two minutes Video. It uses Server Side Capabilities of Nodejs and Spring Boot .

This Web Application Allows A user to upload a two minutes Video. It uses Server Side Capabilities of Nodejs and Spring Boot .

VideoStreamingApplication Purpose Of This Application These days trend of short videos are on rise youtube recently realsed "Shorts" . So , taking ins

Nov 13, 2022
Owner
Divyansh Shekhar Gaur
Software Engineer 1 at NCR Corporation, Hyderabad
Divyansh Shekhar Gaur
A tool ot export, analyse and visualize your transactions, rewards and commissions of your liquidity mining pools or DEX transactions.

VisualTrans A tool ot export, analyse and visualize your transactions, rewards and commissions of your liquidity mining pools or DEX transactions. Doc

Tongjian Cui 15 Mar 11, 2022
Example Project which uses spark mongo connector !

mongo-spark-connector-springboot Example Project which uses spark mongo connector to read/aggregate & convert into Spark DataSet/Java RDDs Connects to

Vibhor 2 Dec 6, 2022
该仓库中主要是 Spring Boot 的入门学习教程以及一些常用的 Spring Boot 实战项目教程,包括 Spring Boot 使用的各种示例代码,同时也包括一些实战项目的项目源码和效果展示,实战项目包括基本的 web 开发以及目前大家普遍使用的线上博客项目/企业大型商城系统/前后端分离实践项目等,摆脱各种 hello world 入门案例的束缚,真正的掌握 Spring Boot 开发。

Spring Boot Projects 该仓库中主要是 Spring Boot 的入门学习教程以及一些常用的 Spring Boot 实战项目教程,包括 Spring Boot 使用的各种示例代码,同时也包括一些实战项目的项目源码和效果展示,实战项目包括基本的 web 开发以及目前大家普遍使用的前

十三 4.5k Dec 30, 2022
Mars - Object Relational Mapping Framework for MongoDB (MongoDB ORM)

Mars Object Relational Mapping Framework for MongoDB 致自己 造自己的轮子,让别人去说 ; What is Mars Mars is a unified driver platform product developed by Shanghai J

null 35 Nov 17, 2022
循序渐进,学习Spring Boot、Spring Boot & Shiro、Spring Batch、Spring Cloud、Spring Cloud Alibaba、Spring Security & Spring Security OAuth2,博客Spring系列源码:https://mrbird.cc

Spring 系列教程 该仓库为个人博客https://mrbird.cc中Spring系列源码,包含Spring Boot、Spring Boot & Shiro、Spring Cloud,Spring Boot & Spring Security & Spring Security OAuth2

mrbird 24.8k Jan 6, 2023
Deploying Spring Boot and MongoDB as Containers Using Docker and Docker Compose

springboot-mongodb-docker Deploying Spring Boot and MongoDB as Containers Using Docker and Docker Compose Steps & Commands pull mongo image from docke

Java Techie 9 Nov 25, 2022
Spring REST API for financial management, developed with Java 11, JWT for authentication, JUnit for unit testing and Oracle Database

control_financial Spring REST API for financial management, developed with Java 11, JWT for authentication, JUnit for unit testing and Oracle Database

Vinicius Cassaro 1 May 27, 2022
This is simple REST API developed with Spring Boot which allows transactions on taboo cards.

Taboo Cards REST API with Spring Boot Summary: This is simple REST API developed with Spring Boot which allows transactions on taboo cards. Requiremen

Alperen Çubuk 2 Sep 16, 2022
This project shows how to configure basic auth to secure our rest API and basic transaction on Data JPA

Basic Atuthentication Spring Boot Data JPA, MySQL This project shows how to configure basic auth to secure our rest API and basic transaction on Data

Hafizullah Samim 1 Feb 10, 2022
r/place replica in Minecraft (Bukkit/Spigot plugin)

MinePlace offers a completely new gaming experience in Minecraft. As a parody of r/place, a world-renowned reddit community project, this project offe

Northernside 4 Aug 19, 2022