How to test the integration between Google Cloud Storage and Spring Boot using Testcontainers
If you recently added an integration between Google Cloud Storage and your Spring Boot application and are wondering how to test it… You came to the right place.
Today we will be learning how to run a Docker image simulating Google Cloud Storage using Testcontainers for our Spring Integration Tests.
For this tutorial we assume you already have a Spring Boot application integrated with Google Cloud Storage and Docker is installed and running in your testing machine.
Testcontainers defines themselves as:
Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.
Google Cloud offers a series of emulators for Testcontainers, such as an emulator for BigQuery or PubSub. Unfortunately, an emulator for Google Cloud Storage is not available.
However, fsouza, a GitHub user, generously made an Open Source library for emulating Google Cloud Storage API. This is the library we will be using and you can check its details here.
Let’s get our hands dirty!
The application we are going to test is responsible for downloading a txt
file from Google Cloud Storage, converting all the characters to upper letters and then reuploading the file back to Google Cloud Storage. Take a look at how it looks:
Now that we have our working code, let’s start working on the tests. As mentioned previously, the first thing we are going to do is configuring Testcontainers to run with the Docker image provided by the fake-gcs-library:
Our Testcontainer is pointing to the Docker image of fake-gcs-library, exposing the port 4443 and running with http:
Let’s configure our initializer, which will start the container and also apply to our application properties the port of the container. This port will be mapped to the property gcs.port
:
Cool! Now let’s override our Google Cloud Storage bean with a custom one pointing to our container instead. Pay attention to how we are using the port we mapped in the previous step:
I’m very happy you got to this part of the story, I‘m truly thankful for this.
Support my work: follow me and clap to this story.
Testing
Let’s create our test now. In this test we will:
- Make sure the bucket is created in our fake gcs server
- Create a new file with the content Hello World
inside
- Upload this file to the input directory inside our fake gcs server
- Wait for this file to be processed by our application
- Download the result file from the output directory inside our fake gcs server
- Assert that the content of the file is HELLO WORLD
And voilà! Our test runs and passes as expected!
I hope you have enjoyed this tutorial. You can also find the implementation of this tutorial in my GitHub by clicking here.
Enjoy coding!
Contribute
Writing takes time and effort. I love writing and sharing knowledge, but I also have bills to pay. If you like my work, please, consider donating through Buy Me a Coffee: https://www.buymeacoffee.com/RaphaelDeLio
Or by sending me BitCoin: 1HjG7pmghg3Z8RATH4aiUWr156BGafJ6Zw
Follow Me on Social Media
Stay connected and dive deeper into the world of Kotlin with me! Follow my journey across all major social platforms for exclusive content, tips, and discussions.