-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserRestControllerUnitTest.java
70 lines (60 loc) · 2.5 KB
/
UserRestControllerUnitTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package be.kdg.programming5.controllers.api;
import be.kdg.programming5.controllers.api.dtos.UpdatedUserDTO;
import be.kdg.programming5.model.Role;
import be.kdg.programming5.model.User;
import be.kdg.programming5.service.users.UserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
import java.time.LocalDate;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class UserRestControllerUnitTest {
@Autowired
private MockMvc mockMvc;
@MockBean
private UserService userService;
@Autowired
private ObjectMapper objectMapper;
@Test
@WithMockUser
void updateUserShouldCallSetNameAndSetBirthdayAndSetRole() throws Exception {
var id = 1L;
var name = "name";
var birthDate = LocalDate.of(2000, 1, 1);
var role = Role.USER;
var user = mock(User.class);
given(userService.getUser(id)).willReturn(Optional.of(user));
given(userService.updateUser(id, name, birthDate, role)).will(invocation -> {
user.setName(name);
user.setBirthdate(birthDate);
user.setRole(role);
return user;
});
mockMvc.perform(put("/api/users/{id}", id)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.content(objectMapper.writeValueAsString(new UpdatedUserDTO(name, birthDate, role)))
.with(csrf()))
.andExpect(status().isOk());
verify(userService).updateUser(id, name, birthDate, role);
verify(user).setName(name);
verify(user).setBirthdate(birthDate);
verify(user).setRole(role);
}
}