Skip to content

Commit f4348ca

Browse files
Ashearapivovarit
authored andcommitted
Test content (eugenp#515)
* Test Model Content * Test get * test get
1 parent 53062a8 commit f4348ca

8 files changed

Lines changed: 193 additions & 13 deletions

File tree

‎spring-mvc-velocity/pom.xml‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@
9595
<version>${powermock.version}</version>
9696
<scope>test</scope>
9797
</dependency>
98+
<dependency>
99+
<groupId>org.springframework</groupId>
100+
<artifactId>spring-test</artifactId>
101+
<version>${org.springframework.version}</version>
102+
<scope>test</scope>
103+
</dependency>
98104

99105
</dependencies>
100106

‎spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/controller/MainController.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import java.util.List;
1212

1313
@Controller
14+
@RequestMapping("/")
1415
public class MainController {
1516

1617
private final TutorialsService tutService;

‎spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/domain/Tutorial.java‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,4 +13,22 @@ public Tutorial(Integer tutId, String title, String description, String author)
1313
this.description = description;
1414
this.author = author;
1515
}
16+
17+
public Integer getTutId() {
18+
return tutId;
19+
}
20+
21+
public String getTitle() {
22+
return title;
23+
}
24+
25+
public String getDescription() {
26+
return description;
27+
}
28+
29+
public String getAuthor() {
30+
return author;
31+
}
32+
33+
1634
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.mvc.velocity.service;
2+
3+
import java.util.List;
4+
5+
import com.baeldung.mvc.velocity.domain.Tutorial;
6+
7+
public interface ITutorialsService {
8+
9+
public List<Tutorial> listTutorials();
10+
}

‎spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/service/TutorialsService.java‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.util.List;
88

99
@Service
10-
public class TutorialsService {
10+
public class TutorialsService implements ITutorialsService {
1111

1212
public List<Tutorial> listTutorials() {
1313
return Arrays.asList(
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.baeldung.mvc.velocity.test;
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+
13+
import java.util.Arrays;
14+
15+
import org.junit.Before;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.mockito.Mockito;
19+
import org.springframework.beans.factory.annotation.Autowired;
20+
import org.springframework.test.context.ContextConfiguration;
21+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
22+
import org.springframework.test.context.web.WebAppConfiguration;
23+
import org.springframework.test.web.servlet.MockMvc;
24+
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
25+
import org.springframework.web.context.WebApplicationContext;
26+
27+
import com.baeldung.mvc.velocity.domain.Tutorial;
28+
import com.baeldung.mvc.velocity.service.ITutorialsService;
29+
import com.baeldung.mvc.velocity.service.TutorialsService;
30+
31+
@RunWith(SpringJUnit4ClassRunner.class)
32+
@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
33+
@WebAppConfiguration
34+
public class DataContentControllerTest {
35+
36+
37+
private MockMvc mockMvc;
38+
39+
@Autowired
40+
private ITutorialsService tutServiceMock;
41+
42+
@Autowired
43+
private WebApplicationContext webApplicationContext;
44+
45+
@Before
46+
public void setUp() {
47+
tutServiceMock = Mockito.mock(TutorialsService.class);
48+
Mockito.reset(tutServiceMock);
49+
50+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
51+
}
52+
53+
@Test
54+
public void testModel() throws Exception{
55+
56+
57+
Mockito.when(tutServiceMock.listTutorials()).thenReturn(Arrays.asList(
58+
new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
59+
new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")
60+
));
61+
62+
mockMvc.perform(get("/"))
63+
.andExpect(status().isOk())
64+
.andExpect(view().name("index"))
65+
.andExpect(model().attribute("tutorials", hasSize(2)))
66+
.andExpect(model().attribute("tutorials", hasItem(
67+
allOf(
68+
hasProperty("tutId", is(1)),
69+
hasProperty("author", is("GuavaAuthor")),
70+
hasProperty("title", is("Guava"))
71+
)
72+
)))
73+
.andExpect(model().attribute("tutorials", hasItem(
74+
allOf(
75+
hasProperty("tutId", is(2)),
76+
hasProperty("author", is("AndroidAuthor")),
77+
hasProperty("title", is("Android"))
78+
)
79+
)));
80+
}
81+
}

‎spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java‎

Lines changed: 38 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,41 @@
11
package com.baeldung.mvc.velocity.test;
22

33

4-
import com.baeldung.mvc.velocity.controller.MainController;
5-
import com.baeldung.mvc.velocity.domain.Tutorial;
6-
import com.baeldung.mvc.velocity.service.TutorialsService;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertNotNull;
6+
import static org.mockito.Mockito.times;
7+
import static org.mockito.Mockito.verify;
8+
import static org.mockito.Mockito.verifyNoMoreInteractions;
9+
10+
import java.util.Arrays;
11+
import java.util.List;
12+
713
import org.junit.Test;
814
import org.junit.runner.RunWith;
915
import org.mockito.Mockito;
10-
import org.mockito.runners.MockitoJUnitRunner;
16+
import org.springframework.test.context.ContextConfiguration;
17+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
18+
import org.springframework.test.context.web.WebAppConfiguration;
1119
import org.springframework.ui.ExtendedModelMap;
1220
import org.springframework.ui.Model;
1321

14-
import java.util.Arrays;
15-
import java.util.List;
16-
17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertNotNull;
22+
import com.baeldung.mvc.velocity.controller.MainController;
23+
import com.baeldung.mvc.velocity.domain.Tutorial;
24+
import com.baeldung.mvc.velocity.service.TutorialsService;
1925

2026

21-
@RunWith(MockitoJUnitRunner.class)
27+
@RunWith(SpringJUnit4ClassRunner.class)
28+
@WebAppConfiguration
29+
@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
2230
public class NavigationControllerTest {
2331

24-
private final MainController mainController = new MainController(Mockito.mock(TutorialsService.class));
32+
private MainController mainController = new MainController(Mockito.mock(TutorialsService.class));
2533

2634
private final Model model = new ExtendedModelMap();
35+
2736

2837
@Test
29-
public final void shouldGoToTutorialListView() {
38+
public void shouldGoToTutorialListView() {
3039
Mockito.when(mainController.getTutService().listTutorials())
3140
.thenReturn(createTutorialList());
3241

@@ -36,7 +45,24 @@ public final void shouldGoToTutorialListView() {
3645
assertEquals("index", view);
3746
assertNotNull(tutorialListAttribute);
3847
}
48+
49+
@Test
50+
public void testContent() throws Exception{
51+
52+
List<Tutorial> tutorials = Arrays.asList(
53+
new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
54+
new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")
55+
);
56+
Mockito.when(mainController.getTutService().listTutorials()).thenReturn(tutorials);
3957

58+
String view = mainController.listTutorialsPage(model);
59+
60+
verify(mainController.getTutService(), times(1)).listTutorials();
61+
verifyNoMoreInteractions(mainController.getTutService());
62+
63+
assertEquals("index", view);
64+
assertEquals(tutorials, model.asMap().get("tutorials"));
65+
}
4066

4167
private static List<Tutorial> createTutorialList() {
4268
return Arrays.asList(new Tutorial(1, "TestAuthor", "Test Title", "Test Description"));
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:context="http://www.springframework.org/schema/context"
5+
xmlns:mvc="http://www.springframework.org/schema/mvc"
6+
xsi:schemaLocation="http://www.springframework.org/schema/beans
7+
http://www.springframework.org/schema/beans/spring-beans.xsd
8+
http://www.springframework.org/schema/context
9+
http://www.springframework.org/schema/context/spring-context-4.1.xsd
10+
http://www.springframework.org/schema/mvc
11+
http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd">
12+
13+
14+
<!-- Register the annotated components in the container eg : annotated controllers -->
15+
<context:component-scan base-package="com.baeldung.mvc.velocity.*" />
16+
17+
<!-- Tell the container that we are going to use annotations -->
18+
<context:annotation-config />
19+
20+
21+
22+
<bean id="velocityConfig"
23+
class="org.springframework.web.servlet.view.velocity.VelocityConfigurer">
24+
<property name="resourceLoaderPath">
25+
<value>/</value>
26+
</property>
27+
</bean>
28+
29+
30+
<bean id="viewResolver"
31+
class="org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver">
32+
<property name="cache" value="true" />
33+
<property name="prefix" value="/WEB-INF/views/" />
34+
<property name="layoutUrl" value="/WEB-INF/layouts/layout.vm" />
35+
<property name="suffix" value=".vm" />
36+
</bean>
37+
38+
</beans>

0 commit comments

Comments
 (0)