Remove springconfig (#538)

* Remove unnecessary code from Velocity example

* view without data

* Removed SpringConfig
This commit is contained in:
Raquel Garrido 2016-07-27 20:03:46 +02:00 committed by Grzegorz Piwowarek
parent de43cc2e5c
commit 1e862df8d4
5 changed files with 18 additions and 43 deletions

View File

@ -18,7 +18,7 @@ public class MainWebAppInitializer implements WebApplicationInitializer {
// Create the 'root' Spring application context // Create the 'root' Spring application context
final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext();
root.register(WebConfig.class, SpringConfig.class); root.register(WebConfig.class);
// Manages the lifecycle of the root application context // Manages the lifecycle of the root application context
sc.addListener(new ContextLoaderListener(root)); sc.addListener(new ContextLoaderListener(root));

View File

@ -1,10 +0,0 @@
package com.baeldung.mvc.velocity.spring.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan(basePackages = { "com.baeldung.mvc.velocity.service" })
public class SpringConfig {
}

View File

@ -13,7 +13,7 @@ import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
@Configuration @Configuration
@EnableWebMvc @EnableWebMvc
@ComponentScan(basePackages = { "com.baeldung.mvc.velocity.controller"}) @ComponentScan(basePackages = { "com.baeldung.mvc.velocity.controller", "com.baeldung.mvc.velocity.service"})
public class WebConfig extends WebMvcConfigurerAdapter { public class WebConfig extends WebMvcConfigurerAdapter {
@Override @Override

View File

@ -1,23 +1,5 @@
package com.baeldung.mvc.velocity.test; 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.spring.config.WebConfig;
import com.baeldung.mvc.velocity.test.config.TestConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
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.allOf;
import static org.hamcrest.Matchers.hasItem; import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasProperty; import static org.hamcrest.Matchers.hasProperty;
@ -29,6 +11,20 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.xpath;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;
import com.baeldung.mvc.velocity.spring.config.WebConfig;
import com.baeldung.mvc.velocity.test.config.TestConfig;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
// @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) // @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"})
@ContextConfiguration(classes = { TestConfig.class, WebConfig.class }) @ContextConfiguration(classes = { TestConfig.class, WebConfig.class })
@ -37,15 +33,12 @@ public class DataContentControllerTest {
private MockMvc mockMvc; private MockMvc mockMvc;
@Autowired
private ITutorialsService tutServiceMock;
@Autowired @Autowired
private WebApplicationContext webApplicationContext; private WebApplicationContext webApplicationContext;
@Before @Before
public void setUp() { public void setUp() {
Mockito.reset(tutServiceMock);
mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
} }
@ -53,8 +46,6 @@ public class DataContentControllerTest {
@Test @Test
public void whenCallingList_ThenModelAndContentOK() throws Exception { public void whenCallingList_ThenModelAndContentOK() 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("/list")).andExpect(status().isOk()).andExpect(view().name("list")).andExpect(model().attribute("tutorials", hasSize(2))) mockMvc.perform(get("/list")).andExpect(status().isOk()).andExpect(view().name("list")).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(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")))))); .andExpect(model().attribute("tutorials", hasItem(allOf(hasProperty("tutId", is(2)), hasProperty("author", is("AndroidAuthor")), hasProperty("title", is("Android"))))));

View File

@ -1,14 +1,11 @@
package com.baeldung.mvc.velocity.test.config; package com.baeldung.mvc.velocity.test.config;
import org.mockito.Mockito;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver; import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.view.velocity.VelocityConfigurer; import org.springframework.web.servlet.view.velocity.VelocityConfigurer;
import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver; import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver;
import com.baeldung.mvc.velocity.service.ITutorialsService;
@Configuration @Configuration
public class TestConfig { public class TestConfig {
@ -30,9 +27,6 @@ public class TestConfig {
return velocityConfigurer; return velocityConfigurer;
} }
@Bean
public ITutorialsService getTutServiceMock() {
return Mockito.mock(ITutorialsService.class);
}
} }