-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathRecipeControllerTest.java
102 lines (80 loc) · 3.28 KB
/
RecipeControllerTest.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
package guru.springframework.controllers;
import guru.springframework.commands.RecipeCommand;
import guru.springframework.domain.Recipe;
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.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import static org.mockito.ArgumentMatchers.any;
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.*;
/**
* Created by jt on 6/19/17.
*/
public class RecipeControllerTest {
@Mock
RecipeService recipeService;
RecipeController controller;
MockMvc mockMvc;
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
controller = new RecipeController(recipeService);
mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
}
@Test
public void testGetRecipe() throws Exception {
Recipe recipe = new Recipe();
recipe.setId(1L);
when(recipeService.findById(anyLong())).thenReturn(recipe);
mockMvc.perform(get("/recipe/1/show"))
.andExpect(status().isOk())
.andExpect(view().name("recipe/show"))
.andExpect(model().attributeExists("recipe"));
}
@Test
public void testGetNewRecipeForm() throws Exception {
RecipeCommand command = new RecipeCommand();
mockMvc.perform(get("/recipe/new"))
.andExpect(status().isOk())
.andExpect(view().name("recipe/recipeform"))
.andExpect(model().attributeExists("recipe"));
}
@Test
public void testPostNewRecipeForm() throws Exception {
RecipeCommand command = new RecipeCommand();
command.setId(2L);
when(recipeService.saveRecipeCommand(any())).thenReturn(command);
mockMvc.perform(post("/recipe")
.contentType(MediaType.APPLICATION_FORM_URLENCODED)
.param("id", "")
.param("description", "some string")
)
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/recipe/2/show"));
}
@Test
public void testGetUpdateView() throws Exception {
RecipeCommand command = new RecipeCommand();
command.setId(2L);
when(recipeService.findCommandById(anyLong())).thenReturn(command);
mockMvc.perform(get("/recipe/1/update"))
.andExpect(status().isOk())
.andExpect(view().name("recipe/recipeform"))
.andExpect(model().attributeExists("recipe"));
}
@Test
public void testDeleteAction() throws Exception {
mockMvc.perform(get("/recipe/1/delete"))
.andExpect(status().is3xxRedirection())
.andExpect(view().name("redirect:/"));
verify(recipeService, times(1)).deleteById(anyLong());
}
}