DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for Unit and Integration test Spring Boot applications with Spring Testing and JUnit
Pavan K Jadda
Pavan K Jadda

Posted on • Updated on

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

This blog post explains the process to write Unit and Integration tests in a 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Β :)

Top comments (0)

Let's Get Wacky


Use any Linode offering to create something unique or silly in the DEV x Linode Hackathon 2022 and win the Wacky Wildcard category

β†’ Join the Hackathon <-