-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathIngredientControllerTest.java
115 lines (91 loc) · 3.92 KB
/
IngredientControllerTest.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package guru.springframework.controllers;
import guru.springframework.commands.IngredientCommand;
import guru.springframework.commands.RecipeCommand;
import guru.springframework.services.IngredientService;
import guru.springframework.services.RecipeService;
import guru.springframework.services.UnitOfMeasureService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import java.util.HashSet;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
public class IngredientControllerTest {
@Mock
IngredientService ingredientService;
@Mock
UnitOfMeasureService unitOfMeasureService;
@Mock
RecipeService recipeService;
IngredientController controller;
MockMvc mockMvc;
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
controller = new IngredientController(ingredientService, recipeService, unitOfMeasureService);
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void testListIngredients() throws Exception {
//given
RecipeCommand recipeCommand = new RecipeCommand();
when(recipeService.findCommandById(anyLong())).thenReturn(recipeCommand);
//when
mockMvc.perform(get("/recipe/1/ingredients"))
.andExpect(status().isOk())
.andExpect(view().name("recipe/ingredient/list"))
.andExpect(model().attributeExists("recipe"));
//then
verify(recipeService, times(1)).findCommandById(anyLong());
}
@Test
public void testShowIngredient() throws Exception {
//given
IngredientCommand ingredientCommand = new IngredientCommand();
//when
when(ingredientService.findByRecipeIdAndIngredientId(anyLong(), anyLong())).thenReturn(ingredientCommand);
//then
mockMvc.perform(get("/recipe/1/ingredient/2/show"))
.andExpect(status().isOk())
.andExpect(view().name("recipe/ingredient/show"))
.andExpect(model().attributeExists("ingredient"));
}
@Test
public void testUpdateIngredientForm() throws Exception {
//given
IngredientCommand ingredientCommand = new IngredientCommand();
//when
when(ingredientService.findByRecipeIdAndIngredientId(anyLong(), anyLong())).thenReturn(ingredientCommand);
when(unitOfMeasureService.listAllUoms()).thenReturn(new HashSet<>());
//then
mockMvc.perform(get("/recipe/1/ingredient/2/update"))
.andExpect(status().isOk())
.andExpect(view().name("recipe/ingredient/ingredientform"))
.andExpect(model().attributeExists("ingredient"))
.andExpect(model().attributeExists("uomList"));
}
@Test
public void testSaveOrUpdate() throws Exception {
//given
IngredientCommand command = new IngredientCommand();
command.setId(3L);
command.setRecipeId(2L);
//when
when(ingredientService.saveIngredientCommand(any())).thenReturn(command);
//then
mockMvc.perform(post("/recipe/2/ingredient")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("id", "")
.param("description", "some string")
)
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/recipe/2/ingredient/3/show"));
}
}