A working application with comprehensive unit and integration tests that serves as a cheat sheet for testing Spring Boot Microservices.

Introduction

I’ve done every type of testing available for Spring Boot Microservices. The patterns are always the same, but sometimes, I forget the details.
You can get AI to write each of the bits individually, but I like having a working build I can play with.

So, I initially set up a simple application and some example tests to remind myself of the configuration and syntax I tend to use.

It’s not a traditional cheat sheet, which would be pages long and too complex.
Instead, it has many working tests as examples to show what you need to do.

I’ve deliberately used different styles and techniques because that tends to be what you encounter in real projects.

It has tests for each component needed in a typical application and checks built-in features such as swagger, health, and info endpoints.

Additionally, it is configured to measure code coverage with Jacoco.

target/site/jacoco

I love the simplicity of Jacoco, and you can only admire its 90’s styling.

Building The Spring Boot Testing Application.

The best way to use this as a cheat sheet is to check it out locally and build it.
https://github.com/johndobie/spring-boot-testing.git

git clone https://github.com/johndobie/spring-boot-testing.git
mvn clean test

Spring Boot Application Architecture.

Understanding the application’s architecture will help you understand why the tests are structured.

It’s pretty standard, if a bit Mickey Mouse regarding the lack of business logic, but it’s close enough.

The remote API is https://jsonplaceholder.typicode.com/posts
This was chosen because it is simple and doesn’t require any authentication.

Component Tests

I won’t worry too much about terminology and have kept things simple.
The tests generally cover one component, starting from its inputs and mocking any dependencies – pretty standard.

For example, a controller test looks like this.

Testing Spring Boot Microservices Cheat Sheet Unit Tests

In the test code, you will see the component under test and a @MockitoBean for each dependency. In this case, the PlaceHolderController is under test, and the PlaceHolderService will be mocked.

@WebMvcTest(PlaceholderController.class)
public class PlaceholderControllerMockMvcTest {
   
    @MockitoBean
    private PlaceholderService placeholderService;
}

Integration Tests

Some of the more complex tests I have marked as integration tests.
These either use something like an in-memory database or test across more than one component.

@DataJpaTest
@AutoConfigureTestDatabase
public class MessageRepositoryIntegrationTest {
 
    @Autowired
    private MessageRepository messageRepository;
}

Test Style

The tests are written in a typical Given When Then (GWT) or Arrange Act Assert(AAA) style with three blocks.

@Test
public void testGetPosts() {
    RemotePost[] remotePosts = TestDataHelper.getRemotePosts();
    given(placeholderClient.getPosts()).willReturn(Arrays.asList(remotePosts));
    
    List<Post> posts = placeholderService.getPosts();
    
    assertThat(posts).isNotEmpty();
    assertThat(posts.size()).isEqualTo(2);
    assertThat(posts.get(0).getTitle()).isEqualTo(TestDataHelper.SAMPLE_TITLE_1);
}

GitHub Builds.

None of this is much use unless it builds.
The builds are here: https://github.com/johndobie/spring-boot-testing/actions

Wrap Up.

The Testing Spring Boot Microservices Cheat Sheet is a work in progress.
There’s a load more tests I can add, and I will add them as I get time.

Links

https://johndobie.com/blog/validation-errors-in-spring-boot-microservices/

https://johndobie.com/blog/git-info-endpoint-in-spring-boot/

https://johndobie.com/blog/java-streams-cheat-sheet-with-examples/