This content originally appeared on DEV Community and was authored by Pavan K Jadda
This blog post explains the process to Unit test and Integration test Spring Boot application with JUnit and Spring Testing library
Unit Tests
Typical Spring Boot application divided into 3 layers
- Controller or Web
- Service
- Repository or DAO
Repository layer Testing
Let's start with Repository layer. See below example.
Spring Boot Repository layer unit TestThe above test class show cases Employee Repository class. We use DataJpaTestannotation
for this. Here are the step by step instructions
- Always use DataJpaTest for Repository later tests
- Disable Auto Configuring Test Database if you want to use existing database
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)
. Otherwise just use@AutoConfigureTestDatabase
- Define Main Class, Properties and Config classes in
@ContextConfiguration
- If you define RefreshScope scope in your code, use @ImportAutoConfiguration(RefreshAutoConfiguration.class) to auto import the config for RefreshScope
- Define or select profile using
@ActiveProfiles(value = "local")
If use password vault like Hashicorp vault, make sure to pass therole_id
andsecret_id
during test start up. See below example for IntelliJ IDE tests
Service Layer Testing
If we want to test service layer, we need to mock Repository layer and build Service class. See below example for EmployeeServiceTest class
The above test class show cases Employee Service class. Here are the step by step instructions
- Use SpringBootTest for Service layer tests and make sure to use
(webEnvironment = RANDOM_PORT)
to let system select random port during start up - Define or select profile using
@ActiveProfiles(value = "local")
- Auto configure MockMvc using
AutoConfigureMockMvc
annotation. Define
TestInstance
Class, to configure the lifecycle of test instances for the annotated test class or test interface. If TestInstance is not explicitly declared on a test class or on a test interface implemented by a test class, the lifecycle mode will implicitly default toPER_METHOD
.If you define RefreshScope scope in your code, use
@ImportAutoConfiguration(RefreshAutoConfiguration.class)
to auto import the config for RefreshScope
And at last If use password vault like Hashicorp vault, make sure to pass therole_id
andsecret_id
during test start up. See the example in Repository layer Test
Controller or Web layer test
The controller or web layer can be tested using MockMvc. See below example
The controller tested using MockMvc
which performs REST API request just like Frontend/Mobile application client. The above request looks similar to Service Layer test with 2 changes
- We are mocking PersonService instead of PersonRepository
- Injected demo user into Spring Security using WithUserDetails
Since the REST API is protected by Spring Security, we use WithUserDetails annotation to mock user demo_user into Spring Security context. Remember this user must exist in Database.
Integration tests
The integration tests look similar to Controller layer tests but with one difference. Instead of mocking the service layer, the test hits actual Service and Repository layer.
Spring Boot Integration TestWe can also define WithUserDetails annotation at method level such that, different users with different access levels can be tested.
Happy Coding :)
This content originally appeared on DEV Community and was authored by Pavan K Jadda
Pavan K Jadda | Sciencx (2021-12-23T15:26:18+00:00) Unit and Integration test Spring Boot applications with Spring Testing and JUnit. Retrieved from https://www.scien.cx/2021/12/23/unit-and-integration-test-spring-boot-applications-with-spring-testing-and-junit/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.