Return to site

🧪🐋 #testcontainers in a #java ☕ #springboot 🍃 project

· spring,java,video

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 👨‍💻

  1. Create JPA entity
  2. Create Spring Data JPA repository
  3. Add schema creation script
  4. Create REST API endpoint
  5. Write test for API endpoint
  6. Run tests
  7. 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

 

All Posts
×

Almost done…

We just sent you an email. Please click the link in the email to confirm your subscription!

OK