diff --git a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/MainWebAppInitializer.java b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/MainWebAppInitializer.java index 6903a662b9..a2871716df 100644 --- a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/MainWebAppInitializer.java +++ b/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/MainWebAppInitializer.java @@ -18,7 +18,7 @@ public class MainWebAppInitializer implements WebApplicationInitializer { // Create the 'root' Spring application context final AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); - root.register(WebConfig.class, SpringConfig.class); + root.register(WebConfig.class); // Manages the lifecycle of the root application context sc.addListener(new ContextLoaderListener(root)); 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 deleted file mode 100644 index 017e1941f1..0000000000 --- a/spring-mvc-velocity/src/main/java/com/baeldung/mvc/velocity/spring/config/SpringConfig.java +++ /dev/null @@ -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 { - -} 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 index 1a3ec62de2..ce8ce1919a 100644 --- 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 @@ -13,7 +13,7 @@ import org.springframework.web.servlet.view.velocity.VelocityLayoutViewResolver; @Configuration @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 { @Override 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 6b6794a653..a9fb242755 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,23 +1,5 @@ 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.hasItem; 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.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) // @ContextConfiguration(locations = {"classpath:mvc-servlet.xml"}) @ContextConfiguration(classes = { TestConfig.class, WebConfig.class }) @@ -37,15 +33,12 @@ public class DataContentControllerTest { private MockMvc mockMvc; - @Autowired - private ITutorialsService tutServiceMock; - @Autowired private WebApplicationContext webApplicationContext; @Before public void setUp() { - Mockito.reset(tutServiceMock); + mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @@ -53,8 +46,6 @@ public class DataContentControllerTest { @Test 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))) .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")))))); 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 index 097900327a..8b84bcdd23 100644 --- 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 @@ -1,14 +1,11 @@ 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 { @@ -30,9 +27,6 @@ public class TestConfig { return velocityConfigurer; } - @Bean - public ITutorialsService getTutServiceMock() { - return Mockito.mock(ITutorialsService.class); - } + }