Improve your system with SpringBoot Integration Testing
Writing tests help us to define the scope of your application and guarantee what you wrote works, right? But remember, tests aren't equal bug-free, but help us to have a lower number of them.
In this article, I'll show the second step of the pyramid, the INTEGRATION aka Integration Testing (IT).
1. What is IT?
Integration testing helps find issues that are not obvious by examining the application’s or a specific unit’s implementation. Integration testing discovers defects in the interplay of several application parts. Sometimes, these defects can be challenging to track or reproduce. (CircleCI)
Integration testing (sometimes called integration and testing, abbreviated I&T) is the phase in software testing in which individual software modules are combined and tested as a group. Integration testing is conducted to evaluate the compliance of a system or component with specified functional requirements.[1] It occurs after unit testing and before system testing. Integration testing takes as its input modules that have been unit tested, groups them in larger aggregates, applies tests defined in an integration test plan to those aggregates, and delivers as its output the integrated system ready for system testing. (Wikipedia)
Integration tests determine if independently developed units of software work correctly when they are connected to each other. The term has become blurred even by the diffuse standards of the software industry, so I’ve been wary of using it in my writing. In particular, many people assume integration tests are necessarily broad in scope, while they can be more effectively done with a narrower scope. (Martin Fowler)
2. Benefits
The source of the benefits is the Art Of Testing site.
- It helps in identifying integration issues between the modules.
- It helps in ensuring that the integrated modules work properly before moving to the system testing of the complete application.
- Bugs found at this level are easier to resolve as compared to the one found at later stages of testing — system and acceptance testing.
- It improves test coverage and provides an additional level of reliability.
3. Drawback
It is very difficult to perform as compared to system testing in which we can consider the application as a black box. Time-consuming — It is very time-consuming and resource-intensive to test all the interfacing between the different connected modules.(Art Of Testing)
4. Hands-on
As we know what IT is, benefits and drawbacks, it's time to code and code well!!
If you want to code step by step, my suggestion is to start using this code. If you want to see the code ready, use this code.
4.1. AS-IS
The AS-IS, is the project before the IT. We can see on the left, 2 folders below the src, only main and test.
On the right, on the verification section, we can see which verification will be done by Gradle.
Until now, nothing related to IT, as expected.
4.2. TO-BE
It's time!!! Let's execute the 4 changes to add IT into the project.
4.2.1. build.gradle
The first change is inside the file build.gradle. In line 7, add the plugin unbroken-dome to make the integration tests easy to be configured.
id 'org.unbroken-dome.test-sets' version '4.0.0'
In line 56 add the code below. Adding the code will enable the task on Gradle.
testSets {
integrationTest
}
This is the complete code 👇
4.2.2. New folder
Add a new folder, with name integrationTest, under the src. All IT will be located here and the specific application configuration as well.
4.2.3. New ITs
In order to be familiar to the application, check the model before
4.2.3.1. GenreIT
This test is pretty straightforward. In line 15, we create a new Genre, and after the Controller is called directly and check the results.
Wait 😟? Where is the database? For this kind of test Spring uses H2 in-memory, and since our application already has this configuration, nothing should be changed and the data will be saved in memory.
4.2.3.2. MovieIT
This test is more complex in comparison with the IT above. Let's analyze the annotations before.
Line 3 → @TestMethodOrder — To guarantee the test execution order. This annotation @TestMethodOrder and the @Order(number) enables the test order.
Line 4 → @DirtiesContext — Clean the context after each test execution
Line 5 → @ActiveProfiles — Which profile will be used in this IT.
Remember, for IT the use of Mocking should be zero 0. Unless you have to integrate with other services, then you can use WireMock.
After the changes your project must look like the screenshot below.
5. Executing the tests
Via console, execute the command below
./gradlew integrationTest
6. Tech Stack
- Java 11
- Gradle 8
- Unbroken-dome 4.0.0
- SpringBoot 2.6.3
- JUnit 5
- GitHub
If you enjoy reading this article, give your clap 👏 and share it.