![]() ![]() But wouldn’t it be too much work? This is where the Testcontainers come into play. When you have complex queries to execute there is no guarantee that passing tests in CI, means that it will work the same in production if you have different database setups.Īlright, then what should I do? Create a separate database just like the one I use in production, populate it with test data and use it only within my test class? Yes, why not? It will fix all your problems. Wait what? I have a PostgreSQL database in my production, and now you are asking me to test with a H2? Since they are both relational database management systems, yes you can use that approach. The easiest would be to add an Java in-memory database like H2 if you are using a SQL database or add an embedded MongoDB database, like the one provided by Flapdoodle if you are using a NoSQL storage. Ok, let’s assume you have decided to test your repository layer. But if you are adding more functionality to your repository than what is provided by spring framework data repository it’s always recommended to test your repositories with integration tests. For simple applications which do not have any custom repository queries this may be true. So, one may argue that we don’t need to test the repository layer at all given the fact that it was not written by us. Since we are using Spring Boot, you would probably be extending an interface such as CrudRepository or MongoRepository based on your choice of database to create the repositories. Hope this will help you to make a better decision next time when you are faced with the same challenge.īefore moving on to writing tests with Testcontainers, let’s try to investigate our options. I’m hoping to share you my experience of writing integration tests to test your repository layer with the use of Testcontainers in this article. Looking for a better way of testing your spring boot repositories? If this is a question that crossed your mind recently, then this article is for you.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |