Spring Boot Unit Test Annotations with Junit 5 -Controller Layer
Hi everybody, we will examine the unit test in spring boot by using Junit . I will cover for all layer (controller,service,repository)
As we know , all of you can write unit test easily because of the simplicity of unit test approaches. But most of us don’t use the all stuff consciously ,especially unit test annotations. But sometimes this can be critical .(performance issues etc.)
So ,lets get started , i can’t wait :)
First of all , i used my demo project to implement the unit test from my github account. If you want to look into details for the project , you can visit my repository:
I guess i need to say our dependencies for our test implementation :)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<version>${spring.boot.test.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
Spring-boot-starter-test is primary dependency that satisfy the main test stuff requirements. H2 embedded database is required so that it eliminates the database configurations for actual database.
- @SpringBootTest: It can be used as an alternative to the standard @ContextConfiguration.This annotation creates the ApplicationContext used in your tests.
- @ExtendWith : Developers can register one or more extensions declaratively by annotating a test interface, test class, test method, or custom composed annotation with @ExtendWith(…) and supplying class references for the extensions to register.
- SpringExtension integrates the Spring TestContext Framework into JUnit 5’s Jupiter programming model.
- @AutoConfigureMockMvc: Annotation that can be applied to a test class to enable and configure auto-configuration of MockMvc. Use AutoConfigureWebMvc if you need to configure the web layer for testing but don’t need to use MockMvc. When you just want to configure mockMvc, then use the AutoConfigureWebMvc.
These annotations are class level annotations , when you have done this part so let’s start inside of the our test class.
@ExtendWith(SpringExtension.class)
@SpringBootTest
@AutoConfigureMockMvc
public class InterviewControllerTest {
...
@MockBean Vs @Mock : Spring boot @MockBean annotation used to add mocks to a Spring ApplicationContext. This class is included in the spring-boot-test,but @Mock annotation is Mockito library’s mock.
As we write a test that doesn’t need any dependencies from the Spring Boot container, the Mockito‘s @Mock shall be used.
Note that to enable Mockito annotations during test executions, the MockitoAnnotations.initMocks(this) static method has to be called.It is fast and favors the isolation of the tested component.
If the test needs to rely on the Spring Boot container and we want also to add or mock one of the container beans then @MockBean from Spring Boot is preferred way to add mocks.We generally use @MockBean along with @WebMvcTest
If you totally understand what mean of these annotations, so you can examine my code snippet .
Note that this test class is supposed to be improved , we need to cover all test case in the real project so that the errors are reduced in production release. This article is partitioned,for example ; Part 2 will cover Service tests, Part 3 will cover repository tests.
If this article is helpful for you, you can follow me and please wait for other articles :)
Here, some useful links for you :