Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
December 23, 2021 03:26 pm GMT

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

  1. Controller or Web
  2. Service
  3. Repository or DAO

Repository layerTesting

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 LayerTesting

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 layertest

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:)


Original Link: https://dev.to/pavankjadda/unit-and-integration-test-spring-boot-applications-with-spring-testing-and-junit-ebb

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To