Writing Integration Tests for a REST API With Kotlin and JUnit 5: Configuration

After we have finished this lesson, we understand how we can eliminate duplicate configuration code when we configure our integration tests.

Get the source code from Github

Configuring Our Integration Tests

We can configure our integration tests by following these steps:

First, because we want to eliminate duplicate configuration code from our integration tests, we have to create a custom annotation that contains the configuration which is shared by our integration tests. We can create this annotation by following these steps:

  1. Create a new annotation class.
  2. Ensure that our custom annotation can be applied to a test class.
  3. Ensure that our custom annotation is stored in binary output and is visible for reflection.
  4. Ensure that our custom annotation is included in the documentation that’s generated from our test classes.
  5. Enable the auto-configuration of the Spring MVC Test framework. When we enable this feature, the Spring TestContext framework creates and configures a MockMvc object that can be injected into our test classes.
  6. Ensure that our custom annotation uses the configuration which we created when we wrote integration tests for a Spring repository.

After we have created the @WebIntegrationTest annotation, its source code looks as follows:

1import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc
2
3@Target(AnnotationTarget.CLASS)
4@Retention(AnnotationRetention.RUNTIME)
5@MustBeDocumented
6@AutoConfigureMockMvc
7@DatabaseIntegrationTest
8annotation class WebIntegrationTest {
9}

Second, when we create a new test class which contains integration tests for a REST API, we have to follow these steps:

  1. Annotate the test class with the @WebIntegrationTest annotation.
  2. Create a private read-only property which contains a MockMvc object and inject the value of this property by using constructor injection. Our request builder object will use this property when it sends HTTP requests to the system under test.
  3. Create a new request builder object and store this object in a private read-only property. Our test functions will use this property when they send HTTP requests to the system under test.

After we have configured the system under test, the source code of our test class looks as follows:

1import org.springframework.beans.factory.annotation.Autowired
2import org.springframework.test.web.servlet.MockMvc
3
4@WebIntegrationTest
5class FindAllTasksTest(@Autowired private val mockMvc: MockMvc) {
6
7    private val requestBuilder = TaskCrudRequestBuilder(mockMvc)
8}

We can now configure our integration tests without writing any duplicate code. Let’s summarize what we learned from this lesson.

Summary

This lesson has taught us one thing:

  • Because we want to configure our integration tests without writing duplicate configuration code, we have to create a custom annotation that contains the configuration which is shared by our integration tests.

Get the source code from Github