mappingConflicts = appServlet.addMapping("/");
+ if (!mappingConflicts.isEmpty()) {
+ throw new IllegalStateException("'appServlet' could not be mapped to '/' due "
+ + "to an existing mapping. This is a known issue under Tomcat versions "
+ + "<= 7.0.14; see https://issues.apache.org/bugzilla/show_bug.cgi?id=51278");
+ }
+ }
+
+}
diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java
new file mode 100644
index 0000000000..a024db543d
--- /dev/null
+++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java
@@ -0,0 +1,15 @@
+package com.baeldung.mvc.velocity.spring.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+
+import com.baeldung.mvc.velocity.service.TutorialsService;
+
+@Configuration
+public class SpringConfig {
+
+ @Bean
+ public TutorialsService tutService(){
+ return new TutorialsService();
+ }
+}
diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/WebConfig.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/WebConfig.java
new file mode 100644
index 0000000000..4cc8e0dca1
--- /dev/null
+++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/WebConfig.java
@@ -0,0 +1,49 @@
+package com.baeldung.mvc.velocity.spring.config;
+
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.ComponentScan;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.ViewResolver;
+import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
+import org.springframework.web.servlet.config.annotation.EnableWebMvc;
+import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
+import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
+import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
+import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
+
+@Configuration
+@EnableWebMvc
+@ComponentScan(basePackages={"com.baeldung.mvc.velocity.controller"})
+public class WebConfig extends WebMvcConfigurerAdapter {
+
+ public WebConfig() {
+ super();
+ }
+
+ @Override
+ public void addResourceHandlers(ResourceHandlerRegistry registry) {
+ registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
+ }
+
+ @Override
+ public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
+ configurer.enable();
+ }
+
+ @Bean
+ public ViewResolver viewResolver() {
+ final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
+ bean.setCache(true);
+ bean.setPrefix("/WEB-INF/views/");
+ bean.setLayoutUrl("/WEB-INF/layouts/layout.vm");
+ bean.setSuffix(".vm");
+ return bean;
+ }
+
+ @Bean
+ public VelocityConfigurer velocityConfig() {
+ VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
+ velocityConfigurer.setResourceLoaderPath("/");
+ return velocityConfigurer;
+ }
+}
diff --git a/spring-mvc-velocity/src/main/webapp/WEB-INF/layouts/layout.vm b/spring-mvc-velocity/src/main/webapp/WEB-INF/layouts/layout.vm
index 203e675cfb..3bac96aaae 100644
--- a/spring-mvc-velocity/src/main/webapp/WEB-INF/layouts/layout.vm
+++ b/spring-mvc-velocity/src/main/webapp/WEB-INF/layouts/layout.vm
@@ -1,6 +1,6 @@
- Spring & Velocity
+ Spring with Velocity
diff --git a/spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm b/spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm
index d1ae0b02cb..9e06a09e4f 100644
--- a/spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm
+++ b/spring-mvc-velocity/src/main/webapp/WEB-INF/views/index.vm
@@ -10,10 +10,10 @@
#foreach($tut in $tutorials)
- | $tut.tutId |
- $tut.title |
- $tut.description |
- $tut.author |
+ $tut.tutId |
+ $tut.title |
+ $tut.description |
+ $tut.author |
#end
\ No newline at end of file
diff --git a/spring-mvc-velocity/src/main/webapp/WEB-INF/web.xml b/spring-mvc-velocity/src/main/webapp/WEB-INF/web_old.xml
similarity index 100%
rename from spring-mvc-velocity/src/main/webapp/WEB-INF/web.xml
rename to spring-mvc-velocity/src/main/webapp/WEB-INF/web_old.xml
diff --git a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java
index 36453eef92..6c62a3fa07 100644
--- a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java
+++ b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/DataContentControllerTest.java
@@ -1,8 +1,18 @@
package com.baeldung.mvc.velocity.test;
-import com.baeldung.mvc.velocity.domain.Tutorial;
-import com.baeldung.mvc.velocity.service.ITutorialsService;
-import com.baeldung.mvc.velocity.service.TutorialsService;
+import static org.hamcrest.Matchers.allOf;
+import static org.hamcrest.Matchers.hasItem;
+import static org.hamcrest.Matchers.hasProperty;
+import static org.hamcrest.Matchers.hasSize;
+import static org.hamcrest.Matchers.is;
+import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
+import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
+
+import java.util.Arrays;
+
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -15,72 +25,48 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
-import java.util.Arrays;
-
-import static org.hamcrest.Matchers.allOf;
-import static org.hamcrest.Matchers.containsString;
-import static org.hamcrest.Matchers.hasItem;
-import static org.hamcrest.Matchers.hasProperty;
-import static org.hamcrest.Matchers.hasSize;
-import static org.hamcrest.Matchers.is;
-import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.model;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
-import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
+import com.baeldung.mvc.velocity.domain.Tutorial;
+import com.baeldung.mvc.velocity.service.ITutorialsService;
+import com.baeldung.mvc.velocity.spring.config.WebConfig;
+import com.baeldung.mvc.velocity.test.config.TestConfig;
@RunWith(SpringJUnit4ClassRunner.class)
-@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
+// @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
+@ContextConfiguration(classes = { TestConfig.class, WebConfig.class })
@WebAppConfiguration
public class DataContentControllerTest {
-
- private MockMvc mockMvc;
+ private MockMvc mockMvc;
- @Autowired
- private ITutorialsService tutServiceMock;
+ @Autowired
+ private ITutorialsService tutServiceMock;
- @Autowired
- private WebApplicationContext webApplicationContext;
-
- @Before
- public void setUp() {
- tutServiceMock = Mockito.mock(TutorialsService.class);
- Mockito.reset(tutServiceMock);
+ @Autowired
+ private WebApplicationContext webApplicationContext;
+
+ @Before
+ public void setUp() {
+ Mockito.reset(tutServiceMock);
+
+ mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
+ }
- mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
- }
-
@Test
- public void testModel() throws Exception{
+ public void testModel() throws Exception {
- Mockito.when(tutServiceMock.listTutorials()).thenReturn(Arrays.asList(
- new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
- new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")
- ));
-
- mockMvc.perform(get("/"))
- .andExpect(status().isOk())
- .andExpect(view().name("index"))
- .andExpect(content().string(containsString("GuavaAuthor")))
- .andExpect(content().string(containsString("Introduction to Guava")))
- .andExpect(content().string(containsString("AndroidAuthor")))
- .andExpect(content().string(containsString("Introduction to Android")))
- .andExpect(model().attribute("tutorials", hasSize(2)))
- .andExpect(model().attribute("tutorials", hasSize(2)))
- .andExpect(model().attribute("tutorials", hasItem(
- allOf(
- hasProperty("tutId", is(1)),
- hasProperty("author", is("GuavaAuthor")),
- hasProperty("title", is("Guava"))
- )
- )))
- .andExpect(model().attribute("tutorials", hasItem(
- allOf(
- hasProperty("tutId", is(2)),
- hasProperty("author", is("AndroidAuthor")),
- hasProperty("title", is("Android"))
- )
- )));
+ Mockito.when(tutServiceMock.listTutorials())
+ .thenReturn(Arrays.asList(new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
+ new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")));
+
+ mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("index"))
+ .andExpect(model().attribute("tutorials", hasSize(2)))
+ .andExpect(model().attribute("tutorials",
+ hasItem(allOf(hasProperty("tutId", is(1)), hasProperty("author", is("GuavaAuthor")),
+ hasProperty("title", is("Guava"))))))
+ .andExpect(model().attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)),
+ hasProperty("author", is("AndroidAuthor")), hasProperty("title", is("Android"))))));
+
+ mockMvc.perform(get("/")).andExpect(xpath("//table").exists());
+ mockMvc.perform(get("/")).andExpect(xpath("//td[@id='tutId_1']").exists());
}
}
diff --git a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java
index e007cf3f94..f9b2cdc76b 100644
--- a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java
+++ b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/NavigationControllerTest.java
@@ -1,9 +1,15 @@
package com.baeldung.mvc.velocity.test;
-import com.baeldung.mvc.velocity.controller.MainController;
-import com.baeldung.mvc.velocity.domain.Tutorial;
-import com.baeldung.mvc.velocity.service.TutorialsService;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+
+import java.util.Arrays;
+import java.util.List;
+
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
@@ -13,18 +19,16 @@ import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model;
-import java.util.Arrays;
-import java.util.List;
+import com.baeldung.mvc.velocity.controller.MainController;
+import com.baeldung.mvc.velocity.domain.Tutorial;
+import com.baeldung.mvc.velocity.service.TutorialsService;
+import com.baeldung.mvc.velocity.test.config.TestConfig;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.mockito.Mockito.times;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyNoMoreInteractions;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
-@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
+//@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
+@ContextConfiguration(classes = {TestConfig.class})
public class NavigationControllerTest {
private MainController mainController = new MainController(Mockito.mock(TutorialsService.class));
@@ -59,7 +63,7 @@ public class NavigationControllerTest {
verifyNoMoreInteractions(mainController.getTutService());
assertEquals("index", view);
- assertEquals(tutorials, model.asMap().get("tutorials"));
+ assertEquals(tutorials, model.asMap().get("tutorials"));
}
private static List createTutorialList() {
diff --git a/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/config/TestConfig.java b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/config/TestConfig.java
new file mode 100644
index 0000000000..097900327a
--- /dev/null
+++ b/spring-mvc-velocity/src/test/java/com/baeldung/mvc/velocity/test/config/TestConfig.java
@@ -0,0 +1,38 @@
+package com.baeldung.mvc.velocity.test.config;
+
+import org.mockito.Mockito;
+import org.springframework.context.annotation.Bean;
+import org.springframework.context.annotation.Configuration;
+import org.springframework.web.servlet.ViewResolver;
+import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
+import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
+
+import com.baeldung.mvc.velocity.service.ITutorialsService;
+
+@Configuration
+public class TestConfig {
+
+
+ @Bean
+ public ViewResolver viewResolver() {
+ final VelocityLayoutViewResolver bean = new VelocityLayoutViewResolver();
+ bean.setCache(true);
+ bean.setPrefix("/WEB-INF/views/");
+ bean.setLayoutUrl("/WEB-INF/layouts/layout.vm");
+ bean.setSuffix(".vm");
+ return bean;
+ }
+
+ @Bean
+ public VelocityConfigurer velocityConfig() {
+ VelocityConfigurer velocityConfigurer = new VelocityConfigurer();
+ velocityConfigurer.setResourceLoaderPath("/");
+ return velocityConfigurer;
+ }
+
+ @Bean
+ public ITutorialsService getTutServiceMock() {
+ return Mockito.mock(ITutorialsService.class);
+ }
+
+}