Unit and Integration test Spring Boot applications with Spring Testing and JUnit

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…


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

  1. Controller or Web
  2. Service
  3. 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

  1. Always use DataJpaTest for Repository later tests
  2. Disable Auto Configuring Test Database if you want to use existing database @AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) . Otherwise just use @AutoConfigureTestDatabase
  3. Define Main Class, Properties and Config classes in @ContextConfiguration
  4. If you define RefreshScope scope in your code, use @ImportAutoConfiguration(RefreshAutoConfiguration.class) to auto import the config for RefreshScope
  5. Define or select profile using @ActiveProfiles(value = "local") If use password vault like Hashicorp vault, make sure to pass the role_id and secret_id during test start up. See below example for IntelliJ IDE tests

IntelliJ Vault Configuration

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

  1. Use SpringBootTest for Service layer tests and make sure to use (webEnvironment = RANDOM_PORT) to let system select random port during start up
  2. Define or select profile using @ActiveProfiles(value = "local")
  3. Auto configure MockMvc using AutoConfigureMockMvc annotation.
  4. 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 to PER_METHOD.

  5. 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 the role_id and secret_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

  1. We are mocking PersonService instead of PersonRepository
  2. 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


Print Share Comment Cite Upload Translate Updates
APA

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/

MLA
" » Unit and Integration test Spring Boot applications with Spring Testing and JUnit." Pavan K Jadda | Sciencx - Thursday December 23, 2021, https://www.scien.cx/2021/12/23/unit-and-integration-test-spring-boot-applications-with-spring-testing-and-junit/
HARVARD
Pavan K Jadda | Sciencx Thursday December 23, 2021 » Unit and Integration test Spring Boot applications with Spring Testing and JUnit., viewed ,<https://www.scien.cx/2021/12/23/unit-and-integration-test-spring-boot-applications-with-spring-testing-and-junit/>
VANCOUVER
Pavan K Jadda | Sciencx - » Unit and Integration test Spring Boot applications with Spring Testing and JUnit. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2021/12/23/unit-and-integration-test-spring-boot-applications-with-spring-testing-and-junit/
CHICAGO
" » Unit and Integration test Spring Boot applications with Spring Testing and JUnit." Pavan K Jadda | Sciencx - Accessed . https://www.scien.cx/2021/12/23/unit-and-integration-test-spring-boot-applications-with-spring-testing-and-junit/
IEEE
" » Unit and Integration test Spring Boot applications with Spring Testing and JUnit." Pavan K Jadda | Sciencx [Online]. Available: https://www.scien.cx/2021/12/23/unit-and-integration-test-spring-boot-applications-with-spring-testing-and-junit/. [Accessed: ]
rf:citation
» Unit and Integration test Spring Boot applications with Spring Testing and JUnit | Pavan K Jadda | Sciencx | 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.

You must be logged in to translate posts. Please log in or register.