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.
This lessons assumes that:
- You can configure the system under test when you are writing integration tests with JUnit 4
- You can create a request builder class that sends HTTP requests to the system under test
- You can configure the system under test when you are writing integration tests for a repository with Kotlin and JUnit 5
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:
- Create a new annotation class.
- Ensure that our custom annotation can be applied to a test class.
- Ensure that our custom annotation is stored in binary output and is visible for reflection.
- Ensure that our custom annotation is included in the documentation that’s generated from our test classes.
- Enable the auto-configuration of the Spring MVC Test framework. When we enable this feature, the Spring TestContext framework creates and configures a
MockMvcobject that can be injected into our test classes. - 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:
- Annotate the test class with the
@WebIntegrationTestannotation. - Create a private read-only property which contains a
MockMvcobject 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. - 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.