Giter VIP home page Giter VIP logo

mongo-starter-test's Introduction

Create & Test Custom MongoDB Data Access Spring Boot Starter Library

This repository is to demonstrate how we can implement & test a custom data access library using Spring Boot Starter. You can find the following code examples here:

  • servlet & reactive implementations of custom Spring Boot Data Access Library for MongoDB
  • how to test the data access libraries using embedded database with TestContainers

For more information, refer to the medium article here!

Usage

Testing the Data Access Library

gradle demo-mvc-mongodb-starter:test        # Test the custom MongoDB Spring Boot Starter Library
gradle demo-reactive-mongodb-starter:test   # Test the custom Reactive MongoDB Spring Boot Starter Library

Run the Demo Service

# Start a MongoDB Server
docker run -d -p 27017:27017 --name mongo-server -e MONGO_INITDB_ROOT_USERNAME=admin -e MONGO_INITDB_ROOT_PASSWORD=admin -e MONGO_INITDB_DATABASE=demoDB mongo:6.0.3

# Run the Demo Service
gradle demo-mvc-service:bootrun             # Spring Web (MVC) 
gradle demo-reactive-service:bootrun        # Spring Webflux (Reactive)

Other Common Issues

Repository Bean Not Found

***************************
APPLICATION FAILED TO START
***************************

Description:
Parameter 0 of constructor in com.example.data.features.demo.DemoDao
required a bean of type 'com.example.data.features.demo.DemoRepository' 
that could not be found.

Action:
Consider defining a bean of type 
'com.example.data.features.demo.DemoRepository' in your configuration.

This probably occurs because the package name for the starter library is different from the Spring Boot application that is consuming it. Eg. com.example.data vs com.example.app. To resolve it, ensure that in the auto-configuration class, add @EnableMongoRepositories.

@Configuration
@EnableMongoRepositories(basePackages = ["com.example.data"])
@ComponentScan("com.example.data")
class MongoAutoConfiguration

DemoDAO has not been initialized

lateinit property demoDao has not been initialized
kotlin.UninitializedPropertyAccessException: lateinit property demoDao has not been initialized

This error occurs when you are running your test cases without @MongoSpringBootTest annotation as the test cases do not know where the packages are found. Eg.

@Configuration
@ComponentScan("com.example.data")
class TestConfig

MongoTemplate Bean Not Found

***************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in com.example.database.dao.DemoDao required a bean named 'mongoTemplate' that could not be found.
Action:
Consider defining a bean named 'mongoTemplate' in your configuration.

This error occurs because the tests cases are not configured with relevant configurations for MongoDB tests. Make sure that in your @MongoSpringBootTest, you add the annotation @AutoConfigureDataMongo. Eg.

@Target(AnnotationTarget.CLASS)
@SpringBootTest(classes = [TestConfig::class])
@ContextConfiguration(initializers = [MongoInitializer::class])
@AutoConfigureDataMongo
annotation class MongoSpringBootTest

References

mongo-starter-test's People

Contributors

bkjam avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.