In this section, we will take a look at how to perform unit testing with components such as controllers, services, and DAOs. We shall also look at some of the features of JUnit 5.
Let's understand some of the following concepts before we get to look at the source codes for controllers and the corresponding unit tests written using JUnit 5:
MockMvc
: This is a Spring MVC component primarily used for writing unit tests with controllers components. Simply speaking,MockMvc
is used to mock the Spring MVC container. The following is what the code looks like when usingMockMvc
:
private MockMvc mockMvc; @MockBean private UserService userService; @BeforeEach public void setUp() throws Exception { this.mockMvc = MockMvcBuilders.standaloneSetup(new UserAccountController(this.userService)).build(); }
In the preceding code, UserService
, a dependency within the controller...