Skip to content

Commit 09d6e18

Browse files
Ashearapivovarit
authored andcommitted
Fix last PR (eugenp#533)
* Test Model Content * Test get * test get * Modified mvc-velocity to java based configuration * Added failOnMissingWebXml to false * Fix config error * fix tests * no message * Revert "fix tests" This reverts commit f2b3942. * Fix PR
1 parent cd2a749 commit 09d6e18

11 files changed

Lines changed: 223 additions & 83 deletions

File tree

‎spring-mvc-velocity/pom.xml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,9 @@
128128
<groupId>org.apache.maven.plugins</groupId>
129129
<artifactId>maven-war-plugin</artifactId>
130130
<version>${maven-war-plugin.version}</version>
131+
<configuration>
132+
<failOnMissingWebXml>false</failOnMissingWebXml>
133+
</configuration>
131134
</plugin>
132135

133136
<plugin>
Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,24 @@
11
package com.baeldung.mvc.velocity.controller;
22

3-
import com.baeldung.mvc.velocity.domain.Tutorial;
4-
import com.baeldung.mvc.velocity.service.TutorialsService;
3+
import java.util.List;
4+
55
import org.springframework.beans.factory.annotation.Autowired;
66
import org.springframework.stereotype.Controller;
77
import org.springframework.ui.Model;
88
import org.springframework.web.bind.annotation.RequestMapping;
99
import org.springframework.web.bind.annotation.RequestMethod;
1010

11-
import java.util.List;
11+
import com.baeldung.mvc.velocity.domain.Tutorial;
12+
import com.baeldung.mvc.velocity.service.ITutorialsService;
1213

1314
@Controller
1415
@RequestMapping("/")
1516
public class MainController {
1617

17-
private final TutorialsService tutService;
18+
private final ITutorialsService tutService;
1819

1920
@Autowired
20-
public MainController(TutorialsService tutService) {
21+
public MainController(ITutorialsService tutService) {
2122
this.tutService = tutService;
2223
}
2324

@@ -28,7 +29,7 @@ public String listTutorialsPage(Model model) {
2829
return "index";
2930
}
3031

31-
public TutorialsService getTutService() {
32+
public ITutorialsService getTutService() {
3233
return tutService;
3334
}
3435
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.baeldung.mvc.velocity.spring.config;
2+
3+
import java.util.Set;
4+
5+
import javax.servlet.ServletContext;
6+
import javax.servlet.ServletException;
7+
import javax.servlet.ServletRegistration;
8+
9+
import org.springframework.web.WebApplicationInitializer;
10+
import org.springframework.web.context.ContextLoaderListener;
11+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
12+
import org.springframework.web.context.support.GenericWebApplicationContext;
13+
import org.springframework.web.servlet.DispatcherServlet;
14+
15+
public class MainWebAppInitializer implements WebApplicationInitializer {
16+
17+
/**
18+
* Register and configure all Servlet container components necessary to
19+
* power the web application.
20+
*/
21+
@Override
22+
public void onStartup(final ServletContext sc) throws ServletException {
23+
24+
// Create the 'root' Spring application context
25+
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
26+
root.register(WebConfig.class, SpringConfig.class);
27+
28+
// Manages the lifecycle of the root application context
29+
sc.addListener(new ContextLoaderListener(root));
30+
31+
// Handles requests into the application
32+
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc",
33+
new DispatcherServlet(new GenericWebApplicationContext()));
34+
appServlet.setLoadOnStartup(1);
35+
36+
final Set<String> mappingConflicts = appServlet.addMapping("/");
37+
if (!mappingConflicts.isEmpty()) {
38+
throw new IllegalStateException("'appServlet' could not be mapped to '/' due "
39+
+ "to an existing mapping. This is a known issue under Tomcat versions "
40+
+ "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
41+
}
42+
}
43+
44+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.baeldung.mvc.velocity.spring.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.Configuration;
5+
6+
import com.baeldung.mvc.velocity.service.TutorialsService;
7+
8+
@Configuration
9+
public class SpringConfig {
10+
11+
@Bean
12+
public TutorialsService tutService(){
13+
return new TutorialsService();
14+
}
15+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package com.baeldung.mvc.velocity.spring.config;
2+
3+
import org.springframework.context.annotation.Bean;
4+
import org.springframework.context.annotation.ComponentScan;
5+
import org.springframework.context.annotation.Configuration;
6+
import org.springframework.web.servlet.ViewResolver;
7+
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
8+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
9+
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
10+
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
11+
import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
12+
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
13+
14+
@Configuration
15+
@EnableWebMvc
16+
@ComponentScan(basePackages={"com.baeldung.mvc.velocity.controller"})
17+
public class WebConfig extends WebMvcConfigurerAdapter {
18+
19+
public WebConfig() {
20+
super();
21+
}
22+
23+
@Override
24+
public void addResourceHandlers(ResourceHandlerRegistry registry) {
25+
registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
26+
}
27+
28+
@Override
29+
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
30+
configurer.enable();
31+
}
32+
33+
@Bean
34+
public ViewResolver viewResolver() {
35+
final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
36+
bean.setCache(true);
37+
bean.setPrefix("/WEB-INF/views/");
38+
bean.setLayoutUrl("/WEB-INF/layouts/layout.vm");
39+
bean.setSuffix(".vm");
40+
return bean;
41+
}
42+
43+
@Bean
44+
public VelocityConfigurer velocityConfig() {
45+
VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
46+
velocityConfigurer.setResourceLoaderPath("/");
47+
return velocityConfigurer;
48+
}
49+
}

‎spring-mvc-velocity/src/main/webapp/WEB-INF/layouts/layout.vm‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<html>
22
<head>
3-
<title>Spring & Velocity</title>
3+
<title>Spring with Velocity</title>
44
</head>
55
<body>
66
<div>

‎spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@
1010
</tr>
1111
#foreach($tut in $tutorials)
1212
<tr>
13-
<td>$tut.tutId</td>
14-
<td>$tut.title</td>
15-
<td>$tut.description</td>
16-
<td>$tut.author</td>
13+
<td id="tutId_$foreach.count">$tut.tutId</td>
14+
<td id="title_$foreach.count">$tut.title</td>
15+
<td id="desc_$foreach.count">$tut.description</td>
16+
<td id="auth_$foreach.count">$tut.author</td>
1717
</tr>
1818
#end
1919
</table>
File renamed without changes.
Lines changed: 46 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
package com.baeldung.mvc.velocity.test;
22

3-
import com.baeldung.mvc.velocity.domain.Tutorial;
4-
import com.baeldung.mvc.velocity.service.ITutorialsService;
5-
import com.baeldung.mvc.velocity.service.TutorialsService;
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+
616
import org.junit.Before;
717
import org.junit.Test;
818
import org.junit.runner.RunWith;
@@ -15,72 +25,48 @@
1525
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
1626
import org.springframework.web.context.WebApplicationContext;
1727

18-
import java.util.Arrays;
19-
20-
import static org.hamcrest.Matchers.allOf;
21-
import static org.hamcrest.Matchers.containsString;
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.content;
28-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
29-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
30-
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
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;
3132

3233
@RunWith(SpringJUnit4ClassRunner.class)
33-
@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
34+
// @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
35+
@ContextConfiguration(classes = { TestConfig.class, WebConfig.class })
3436
@WebAppConfiguration
3537
public class DataContentControllerTest {
36-
3738

38-
private MockMvc mockMvc;
39+
private MockMvc mockMvc;
40+
41+
@Autowired
42+
private ITutorialsService tutServiceMock;
3943

40-
@Autowired
41-
private ITutorialsService tutServiceMock;
44+
@Autowired
45+
private WebApplicationContext webApplicationContext;
4246

43-
@Autowired
44-
private WebApplicationContext webApplicationContext;
45-
46-
@Before
47-
public void setUp() {
48-
tutServiceMock = Mockito.mock(TutorialsService.class);
49-
Mockito.reset(tutServiceMock);
47+
@Before
48+
public void setUp() {
49+
Mockito.reset(tutServiceMock);
50+
51+
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
52+
}
5053

51-
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
52-
}
53-
5454
@Test
55-
public void testModel() throws Exception{
55+
public void testModel() throws Exception {
56+
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")));
60+
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"))))));
5668

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(content().string(containsString("GuavaAuthor")))
66-
.andExpect(content().string(containsString("Introduction to Guava")))
67-
.andExpect(content().string(containsString("AndroidAuthor")))
68-
.andExpect(content().string(containsString("Introduction to Android")))
69-
.andExpect(model().attribute("tutorials", hasSize(2)))
70-
.andExpect(model().attribute("tutorials", hasSize(2)))
71-
.andExpect(model().attribute("tutorials", hasItem(
72-
allOf(
73-
hasProperty("tutId", is(1)),
74-
hasProperty("author", is("GuavaAuthor")),
75-
hasProperty("title", is("Guava"))
76-
)
77-
)))
78-
.andExpect(model().attribute("tutorials", hasItem(
79-
allOf(
80-
hasProperty("tutId", is(2)),
81-
hasProperty("author", is("AndroidAuthor")),
82-
hasProperty("title", is("Android"))
83-
)
84-
)));
69+
mockMvc.perform(get("/")).andExpect(xpath("//table").exists());
70+
mockMvc.perform(get("/")).andExpect(xpath("//td[@id='tutId_1']").exists());
8571
}
8672
}

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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
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;
@@ -13,18 +19,16 @@
1319
import org.springframework.ui.ExtendedModelMap;
1420
import org.springframework.ui.Model;
1521

16-
import java.util.Arrays;
17-
import java.util.List;
22+
import com.baeldung.mvc.velocity.controller.MainController;
23+
import com.baeldung.mvc.velocity.domain.Tutorial;
24+
import com.baeldung.mvc.velocity.service.TutorialsService;
25+
import com.baeldung.mvc.velocity.test.config.TestConfig;
1826

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertNotNull;
21-
import static org.mockito.Mockito.times;
22-
import static org.mockito.Mockito.verify;
23-
import static org.mockito.Mockito.verifyNoMoreInteractions;
2427

2528
@RunWith(SpringJUnit4ClassRunner.class)
2629
@WebAppConfiguration
27-
@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
30+
//@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
31+
@ContextConfiguration(classes = {TestConfig.class})
2832
public class NavigationControllerTest {
2933

3034
private MainController mainController = new MainController(Mockito.mock(TutorialsService.class));
@@ -59,7 +63,7 @@ public void testContent() throws Exception{
5963
verifyNoMoreInteractions(mainController.getTutService());
6064

6165
assertEquals("index", view);
62-
assertEquals(tutorials, model.asMap().get("tutorials"));
66+
assertEquals(tutorials, model.asMap().get("tutorials"));
6367
}
6468

6569
private static List<Tutorial> createTutorialList() {

0 commit comments

Comments
 (0)