1) definition test container by testcontainers.com ๐ฉโ๐ผ
Testcontainers is an open source framework for providing throwaway, lightweight instances of databases, message brokers, web browsers, or just about anything that can run in a Docker container.
2) Basic example by chatGPT ๐ค
3) Demo app without testcontainers ๐จโ๐ป
4) SpringBoot example by testcontainers.com ๐จโ๐ป
- Create JPA entity
- Create Spring Data JPA repository
- Add schema creation script
- Create REST API endpoint
- Write test for API endpoint
- Run tests
- Summary
The Testcontainers library helped us to write integration tests by using the same type of database, Postgres, that we use in production as opposed to Mocks or in-memory databases.
As we are not using mocks and talking to the real services, we are free to do any code refactoring and still ensure that the application is working as expected.
My GitHub repo:
5) Conclusion with Reddit comments ๐ฃ๏ธ
* It also reduces the amount of testing code enormously: no more mocks !
* Testcontainers made my life so much easier!
*Do people usually use testcontainers in unit test?
Integration Tests
*Literal game changer!
*Previously we used h2 for unit testing that required dealing with databases.
Since testcontainers, we can have a REAL mysql...
*my senior asked to never use testcontainers ever exactly because it doesnt make things easier but more complicated.
Everybody on the internet tells that should use docker:dind and then it is easy to run,
but it actually make things worse because you are suposted to run runner in privileged mode which is unsafe.
After realising that we went for old mocking solution also.
*This actually goes against the trend in the Java ecosystem to use Testcontainers. Ising for example H2 causes a lot of issues because it simply doesnโt behave the same way.
Testcontainers is quite fast. It doesnโt restart for every test.
6) Spring pro Udemy course ๐จโ๐ซ
Spring Professional Certification - 6 full Tests (2V0-72.22)
NEW SPRING Certification prep with 360 questions/answers from the best of Spring Certified Professional -- 2023 Edition