|
1 | 1 | package com.baeldung.mvc.velocity.test; |
2 | 2 |
|
3 | | -import static org.hamcrest.Matchers.allOf; |
4 | | -import static org.hamcrest.Matchers.hasItem; |
5 | | -import static org.hamcrest.Matchers.hasProperty; |
6 | | -import static org.hamcrest.Matchers.hasSize; |
7 | | -import static org.hamcrest.Matchers.is; |
8 | | -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
9 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; |
10 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
11 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; |
12 | | -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath; |
13 | | - |
14 | | -import java.util.Arrays; |
15 | | - |
| 3 | +import com.baeldung.mvc.velocity.domain.Tutorial; |
| 4 | +import com.baeldung.mvc.velocity.service.ITutorialsService; |
| 5 | +import com.baeldung.mvc.velocity.spring.config.WebConfig; |
| 6 | +import com.baeldung.mvc.velocity.test.config.TestConfig; |
16 | 7 | import org.junit.Before; |
17 | 8 | import org.junit.Test; |
18 | 9 | import org.junit.runner.RunWith; |
|
25 | 16 | import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
26 | 17 | import org.springframework.web.context.WebApplicationContext; |
27 | 18 |
|
28 | | -import com.baeldung.mvc.velocity.domain.Tutorial; |
29 | | -import com.baeldung.mvc.velocity.service.ITutorialsService; |
30 | | -import com.baeldung.mvc.velocity.spring.config.WebConfig; |
31 | | -import com.baeldung.mvc.velocity.test.config.TestConfig; |
| 19 | +import java.util.Arrays; |
| 20 | + |
| 21 | +import static org.hamcrest.Matchers.allOf; |
| 22 | +import static org.hamcrest.Matchers.hasItem; |
| 23 | +import static org.hamcrest.Matchers.hasProperty; |
| 24 | +import static org.hamcrest.Matchers.hasSize; |
| 25 | +import static org.hamcrest.Matchers.is; |
| 26 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; |
| 27 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model; |
| 28 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 29 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; |
| 30 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath; |
32 | 31 |
|
33 | 32 | @RunWith(SpringJUnit4ClassRunner.class) |
34 | 33 | // @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) |
35 | | -@ContextConfiguration(classes = { TestConfig.class, WebConfig.class }) |
36 | | -@WebAppConfiguration |
37 | | -public class DataContentControllerTest { |
| 34 | +@ContextConfiguration(classes = { TestConfig.class, WebConfig.class }) @WebAppConfiguration public class DataContentControllerTest { |
38 | 35 |
|
39 | | - private MockMvc mockMvc; |
| 36 | + private MockMvc mockMvc; |
40 | 37 |
|
41 | | - @Autowired |
42 | | - private ITutorialsService tutServiceMock; |
| 38 | + @Autowired private ITutorialsService tutServiceMock; |
43 | 39 |
|
44 | | - @Autowired |
45 | | - private WebApplicationContext webApplicationContext; |
| 40 | + @Autowired private WebApplicationContext webApplicationContext; |
46 | 41 |
|
47 | | - @Before |
48 | | - public void setUp() { |
49 | | - Mockito.reset(tutServiceMock); |
| 42 | + @Before public void setUp() { |
| 43 | + Mockito.reset(tutServiceMock); |
50 | 44 |
|
51 | | - mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); |
52 | | - } |
| 45 | + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); |
| 46 | + } |
53 | 47 |
|
54 | | - @Test |
55 | | - public void testModel() throws Exception { |
| 48 | + @Test public void testModel() throws Exception { |
56 | 49 |
|
57 | | - Mockito.when(tutServiceMock.listTutorials()) |
58 | | - .thenReturn(Arrays.asList(new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), |
59 | | - new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor"))); |
| 50 | + Mockito.when(tutServiceMock.listTutorials()) |
| 51 | + .thenReturn(Arrays.asList( |
| 52 | + new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), |
| 53 | + new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor"))); |
60 | 54 |
|
61 | | - mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("index")) |
62 | | - .andExpect(model().attribute("tutorials", hasSize(2))) |
63 | | - .andExpect(model().attribute("tutorials", |
64 | | - hasItem(allOf(hasProperty("tutId", is(1)), hasProperty("author", is("GuavaAuthor")), |
65 | | - hasProperty("title", is("Guava")))))) |
66 | | - .andExpect(model().attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)), |
67 | | - hasProperty("author", is("AndroidAuthor")), hasProperty("title", is("Android")))))); |
| 55 | + mockMvc.perform(get("/")) |
| 56 | + .andExpect(status().isOk()).andExpect(view().name("index")) |
| 57 | + .andExpect(model().attribute("tutorials", hasSize(2))) |
| 58 | + .andExpect(model() |
| 59 | + .attribute("tutorials", |
| 60 | + hasItem(allOf(hasProperty("tutId", is(1)), |
| 61 | + hasProperty("author", is("GuavaAuthor")), |
| 62 | + hasProperty("title", is("Guava")))))) |
| 63 | + .andExpect(model() |
| 64 | + .attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)), |
| 65 | + hasProperty("author", is("AndroidAuthor")), |
| 66 | + hasProperty("title", is("Android")))))); |
68 | 67 |
|
69 | | - mockMvc.perform(get("/")).andExpect(xpath("//table").exists()); |
70 | | - mockMvc.perform(get("/")).andExpect(xpath("//td[@id='tutId_1']").exists()); |
71 | | - } |
| 68 | + mockMvc.perform(get("/")) |
| 69 | + .andExpect(xpath("//table").exists()); |
| 70 | + mockMvc.perform(get("/")) |
| 71 | + .andExpect(xpath("//td[@id='tutId_1']").exists()); |
| 72 | + } |
72 | 73 | } |
0 commit comments