Fix last PR (#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 f2b39424cf718d274d7dc82a4e5fc89e703b8aaf.

* Fix PR
This commit is contained in:
Raquel Garrido 2016-07-26 08:04:19 +02:00 committed by Grzegorz Piwowarek
parent cd2a749cd4
commit 09d6e1852f
11 changed files with 223 additions and 83 deletions

View File

@ -128,6 +128,9 @@
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId> <artifactId>maven-war-plugin</artifactId>
<version>${maven-war-plugin.version}</version> <version>${maven-war-plugin.version}</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin> </plugin>
<plugin> <plugin>

View File

@ -1,23 +1,24 @@
package com.baeldung.mvc.velocity.controller; package com.baeldung.mvc.velocity.controller;
import com.baeldung.mvc.velocity.domain.Tutorial; import java.util.List;
import com.baeldung.mvc.velocity.service.TutorialsService;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod;
import java.util.List; import com.baeldung.mvc.velocity.domain.Tutorial;
import com.baeldung.mvc.velocity.service.ITutorialsService;
@Controller @Controller
@RequestMapping("/") @RequestMapping("/")
public class MainController { public class MainController {
private final TutorialsService tutService; private final ITutorialsService tutService;
@Autowired @Autowired
public MainController(TutorialsService tutService) { public MainController(ITutorialsService tutService) {
this.tutService = tutService; this.tutService = tutService;
} }
@ -28,7 +29,7 @@ public class MainController {
return "index"; return "index";
} }
public TutorialsService getTutService() { public ITutorialsService getTutService() {
return tutService; return tutService;
} }
} }

View File

@ -0,0 +1,44 @@
package com.baeldung.mvc.velocity.spring.config;
import java.util.Set;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
public class MainWebAppInitializer implements WebApplicationInitializer {
/**
* Register and configure all Servlet container components necessary to
* power the web application.
*/
@Override
public void onStartup(final ServletContext sc) throws ServletException {
// Create the 'root' Spring application context
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.register(WebConfig.class, SpringConfig.class);
// Manages the lifecycle of the root application context
sc.addListener(new ContextLoaderListener(root));
// Handles requests into the application
final ServletRegistration.Dynamic appServlet = sc.addServlet("mvc",
new DispatcherServlet(new GenericWebApplicationContext()));
appServlet.setLoadOnStartup(1);
final Set<String> 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");
}
}
}

View File

@ -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();
}
}

View File

@ -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;
}
}

View File

@ -1,6 +1,6 @@
<html> <html>
<head> <head>
<title>Spring & Velocity</title> <title>Spring with Velocity</title>
</head> </head>
<body> <body>
<div> <div>

View File

@ -10,10 +10,10 @@
</tr> </tr>
#foreach($tut in $tutorials) #foreach($tut in $tutorials)
<tr> <tr>
<td>$tut.tutId</td> <td id="tutId_$foreach.count">$tut.tutId</td>
<td>$tut.title</td> <td id="title_$foreach.count">$tut.title</td>
<td>$tut.description</td> <td id="desc_$foreach.count">$tut.description</td>
<td>$tut.author</td> <td id="auth_$foreach.count">$tut.author</td>
</tr> </tr>
#end #end
</table> </table>

View File

@ -1,8 +1,18 @@
package com.baeldung.mvc.velocity.test; package com.baeldung.mvc.velocity.test;
import com.baeldung.mvc.velocity.domain.Tutorial; import static org.hamcrest.Matchers.allOf;
import com.baeldung.mvc.velocity.service.ITutorialsService; import static org.hamcrest.Matchers.hasItem;
import com.baeldung.mvc.velocity.service.TutorialsService; 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.Before;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
@ -15,26 +25,17 @@ import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders; import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.WebApplicationContext;
import java.util.Arrays; import com.baeldung.mvc.velocity.domain.Tutorial;
import com.baeldung.mvc.velocity.service.ITutorialsService;
import static org.hamcrest.Matchers.allOf; import com.baeldung.mvc.velocity.spring.config.WebConfig;
import static org.hamcrest.Matchers.containsString; import com.baeldung.mvc.velocity.test.config.TestConfig;
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;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) // @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
@ContextConfiguration(classes = { TestConfig.class, WebConfig.class })
@WebAppConfiguration @WebAppConfiguration
public class DataContentControllerTest { public class DataContentControllerTest {
private MockMvc mockMvc; private MockMvc mockMvc;
@Autowired @Autowired
@ -45,42 +46,27 @@ public class DataContentControllerTest {
@Before @Before
public void setUp() { public void setUp() {
tutServiceMock = Mockito.mock(TutorialsService.class);
Mockito.reset(tutServiceMock); Mockito.reset(tutServiceMock);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
} }
@Test @Test
public void testModel() throws Exception{ public void testModel() throws Exception {
Mockito.when(tutServiceMock.listTutorials()).thenReturn(Arrays.asList( Mockito.when(tutServiceMock.listTutorials())
new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"), .thenReturn(Arrays.asList(new Tutorial(1, "Guava", "Introduction to Guava", "GuavaAuthor"),
new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor") new Tutorial(2, "Android", "Introduction to Android", "AndroidAuthor")));
));
mockMvc.perform(get("/")) mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(view().name("index"))
.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", hasSize(2))) .andExpect(model().attribute("tutorials",
.andExpect(model().attribute("tutorials", hasItem( hasItem(allOf(hasProperty("tutId", is(1)), hasProperty("author", is("GuavaAuthor")),
allOf( hasProperty("title", is("Guava"))))))
hasProperty("tutId", is(1)), .andExpect(model().attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)),
hasProperty("author", is("GuavaAuthor")), hasProperty("author", is("AndroidAuthor")), hasProperty("title", is("Android"))))));
hasProperty("title", is("Guava"))
) mockMvc.perform(get("/")).andExpect(xpath("//table").exists());
))) mockMvc.perform(get("/")).andExpect(xpath("//td[@id='tutId_1']").exists());
.andExpect(model().attribute("tutorials", hasItem(
allOf(
hasProperty("tutId", is(2)),
hasProperty("author", is("AndroidAuthor")),
hasProperty("title", is("Android"))
)
)));
} }
} }

View File

@ -1,9 +1,15 @@
package com.baeldung.mvc.velocity.test; package com.baeldung.mvc.velocity.test;
import com.baeldung.mvc.velocity.controller.MainController; import static org.junit.Assert.assertEquals;
import com.baeldung.mvc.velocity.domain.Tutorial; import static org.junit.Assert.assertNotNull;
import com.baeldung.mvc.velocity.service.TutorialsService; 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.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -13,18 +19,16 @@ import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.ui.ExtendedModelMap; import org.springframework.ui.ExtendedModelMap;
import org.springframework.ui.Model; import org.springframework.ui.Model;
import java.util.Arrays; import com.baeldung.mvc.velocity.controller.MainController;
import java.util.List; 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) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) //@ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
@ContextConfiguration(classes = {TestConfig.class})
public class NavigationControllerTest { public class NavigationControllerTest {
private MainController mainController = new MainController(Mockito.mock(TutorialsService.class)); private MainController mainController = new MainController(Mockito.mock(TutorialsService.class));

View File

@ -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);
}
}