-
Notifications
You must be signed in to change notification settings - Fork 4.2k
/
Copy pathIndexControllerTest.java
80 lines (59 loc) · 2.25 KB
/
IndexControllerTest.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
package guru.springframework.controllers;
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.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.ui.Model;
import java.util.HashSet;
import java.util.Set;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
/**
* Created by jt on 6/17/17.
*/
public class IndexControllerTest {
@Mock
RecipeService recipeService;
@Mock
Model model;
IndexController controller;
@BeforeEach
public void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
controller = new IndexController(recipeService);
}
@Test
public void testMockMVC() throws Exception {
MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build();
mockMvc.perform(get("/"))
.andExpect(status().isOk())
.andExpect(view().name("index"));
}
@Test
public void getIndexPage() throws Exception {
//given
Set<Recipe> recipes = new HashSet<>();
recipes.add(new Recipe());
Recipe recipe = new Recipe();
recipe.setId(1L);
recipes.add(recipe);
when(recipeService.getRecipes()).thenReturn(recipes);
ArgumentCaptor<Set<Recipe>> argumentCaptor = ArgumentCaptor.forClass(Set.class);
//when
String viewName = controller.getIndexPage(model);
//then
assertEquals("index", viewName);
verify(recipeService, times(1)).getRecipes();
verify(model, times(1)).addAttribute(eq("recipes"), argumentCaptor.capture());
Set<Recipe> setInController = argumentCaptor.getValue();
assertEquals(2, setInController.size());
}
}