-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathImageControllerTest.java
71 lines (51 loc) · 2.21 KB
/
ImageControllerTest.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
71
package guru.springframework.controllers;
import guru.springframework.commands.RecipeCommand;
import guru.springframework.services.ImageService;
import guru.springframework.services.RecipeService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.mock.web.MockMultipartFile;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.multipart;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class ImageControllerTest {
@Mock
ImageService imageService;
@Mock
RecipeService recipeService;
ImageController controller;
MockMvc mockMvc;
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
controller = new ImageController(imageService, recipeService);
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void getImageForm() throws Exception {
//given
RecipeCommand command = new RecipeCommand();
command.setId(1L);
when(recipeService.findCommandById(anyLong())).thenReturn(command);
//when
mockMvc.perform(get("/recipe/1/image"))
.andExpect(status().isOk())
.andExpect(model().attributeExists("recipe"));
verify(recipeService, times(1)).findCommandById(anyLong());
}
@Test
public void handleImagePost() throws Exception {
MockMultipartFile multipartFile =
new MockMultipartFile("imagefile", "testing.txt", "text/plain",
"Spring Framework Guru".getBytes());
mockMvc.perform(multipart("/recipe/1/image").file(multipartFile))
.andExpect(status().is3xxRedirection())
.andExpect(header().string("Location", "/recipe/1/show"));
verify(imageService, times(1)).saveImageFile(anyLong(), any());
}
}