BAEL-97 - simplifying code

This commit is contained in:
slavisa-baeldung 2017-01-05 08:29:54 +01:00
parent ae141415bc
commit d5f3eaa77a
24 changed files with 86 additions and 416 deletions

View File

@ -1,27 +0,0 @@
package com.baeldung.controller;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;
import com.baeldung.config.WebAppConfiguration_BeanNameUrlHandlerMapping;
public class WebAppInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext container) throws ServletException {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
ctx.register(WebAppConfiguration_BeanNameUrlHandlerMapping.class);
ctx.setServletContext(container);
ServletRegistration.Dynamic servlet = container.addServlet("dispatcher", new DispatcherServlet(ctx));
servlet.setLoadOnStartup(1);
servlet.addMapping("/");
}
}

View File

@ -1,18 +0,0 @@
package com.baeldung.controller;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class WelcomeController extends AbstractController {
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Inside Welcome Controller");
ModelAndView model = new ModelAndView("welcome");
return model;
}
}

View File

@ -1,34 +0,0 @@
package com.baeldung.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.baeldung.controller.WelcomeController;
@Configuration
public class WebAppConfiguration_ControllerClassNameHandlerMapping {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public ControllerClassNameHandlerMapping controllerClassNameHandlerMapping() {
ControllerClassNameHandlerMapping controllerClassNameHandlerMapping = new ControllerClassNameHandlerMapping();
return controllerClassNameHandlerMapping;
}
@Bean
public WelcomeController welcome() {
WelcomeController welcome = new WelcomeController();
return welcome;
}
}

View File

@ -1,41 +0,0 @@
package com.baeldung.config;
import java.util.HashMap;
import java.util.Map;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.baeldung.controller.WelcomeController;
@Configuration
public class WebAppConfiguration_SimpleUrlHandlerMapping {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
Map<String, Object> urlMap = new HashMap<>();
urlMap.put("/simpleUrlWelcome", welcome());
simpleUrlHandlerMapping.setUrlMap(urlMap);
return simpleUrlHandlerMapping;
}
@Bean
public WelcomeController welcome() {
WelcomeController welcome = new WelcomeController();
return welcome;
}
}

View File

@ -1,41 +0,0 @@
package com.baeldung.spring.web.config;
import com.baeldung.web.controller.handlermapping.ExampleTwoController;
import com.baeldung.web.controller.handlermapping.WelcomeController;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
@Configuration
public class BeanNameHandlerMappingConfig {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
viewResolver.setPrefix("/");
viewResolver.setSuffix(".jsp");
return viewResolver;
}
@Bean
public BeanNameUrlHandlerMapping controllerClassNameHandlerMapping() {
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping();
return beanNameUrlHandlerMapping;
}
@Bean(name="/welcomeBean")
public WelcomeController welcomeBean() {
WelcomeController welcome = new WelcomeController();
return welcome;
}
@Bean(name="/ex")
public ExampleTwoController exampleTwo() {
ExampleTwoController exampleTwo = new ExampleTwoController();
return exampleTwo;
}
}

View File

@ -1,15 +1,14 @@
package com.baeldung.config; package com.baeldung.spring.web.config;
import com.baeldung.web.controller.handlermapping.WelcomeController;
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.handler.BeanNameUrlHandlerMapping; import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver;
import com.baeldung.controller.WelcomeController;
@Configuration @Configuration
public class WebAppConfiguration_BeanNameUrlHandlerMapping { public class BeanNameUrlHandlerMappingConfig {
@Bean @Bean
public ViewResolver viewResolver() { public ViewResolver viewResolver() {
@ -21,14 +20,12 @@ public class WebAppConfiguration_BeanNameUrlHandlerMapping {
@Bean @Bean
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() { BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
BeanNameUrlHandlerMapping bean = new BeanNameUrlHandlerMapping(); return new BeanNameUrlHandlerMapping();
return bean;
} }
@Bean("/beanNameUrl") @Bean("/beanNameUrl")
public WelcomeController welcome() { public WelcomeController welcome() {
WelcomeController welcome = new WelcomeController(); return new WelcomeController();
return welcome;
} }
} }

View File

@ -1,15 +1,14 @@
package com.baeldung.spring.web.config; package com.baeldung.spring.web.config;
import com.baeldung.web.controller.handlermapping.ExampleTwoController;
import com.baeldung.web.controller.handlermapping.WelcomeController; import com.baeldung.web.controller.handlermapping.WelcomeController;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
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.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping; import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver;
@EnableWebMvc
@Configuration @Configuration
public class ControllerClassNameHandlerMappingConfig { public class ControllerClassNameHandlerMappingConfig {
@ -29,14 +28,8 @@ public class ControllerClassNameHandlerMappingConfig {
@Bean @Bean
public WelcomeController welcome() { public WelcomeController welcome() {
WelcomeController welcome = new WelcomeController(); return new WelcomeController();
return welcome;
} }
@Bean
public ExampleTwoController exampleTwo() {
ExampleTwoController exampleTwo = new ExampleTwoController();
return exampleTwo;
}
} }

View File

@ -1,20 +1,20 @@
package com.baeldung.config; package com.baeldung.spring.web.config;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import com.baeldung.web.controller.handlermapping.BaeldungController;
import com.baeldung.web.controller.handlermapping.TestController;
import com.baeldung.web.controller.handlermapping.WelcomeController;
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.handler.BeanNameUrlHandlerMapping; import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping; import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping;
import com.baeldung.controller.BaeldungController;
import com.baeldung.controller.WelcomeController;
import com.baeldung.controller.TestController;
@Configuration @Configuration
public class WebAppConfiguration_ConfiguringPriorities { public class HandlerMappingPrioritiesConfig {
@Bean @Bean
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() { BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {

View File

@ -1,6 +1,8 @@
package com.baeldung.spring.web.config; package com.baeldung.spring.web.config;
import com.baeldung.web.controller.handlermapping.ExampleTwoController; import java.util.HashMap;
import java.util.Map;
import com.baeldung.web.controller.handlermapping.WelcomeController; import com.baeldung.web.controller.handlermapping.WelcomeController;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
@ -8,8 +10,9 @@ import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
import org.springframework.web.servlet.view.InternalResourceViewResolver; import org.springframework.web.servlet.view.InternalResourceViewResolver;
import java.util.HashMap;
import java.util.Map;
@Configuration @Configuration
public class SimpleUrlHandlerMappingConfig { public class SimpleUrlHandlerMappingConfig {
@ -27,21 +30,14 @@ public class SimpleUrlHandlerMappingConfig {
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping(); SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
Map<String, Object> urlMap = new HashMap<>(); Map<String, Object> urlMap = new HashMap<>();
urlMap.put("/simpleUrlWelcome", welcome()); urlMap.put("/simpleUrlWelcome", welcome());
urlMap.put("/exampleTwo", exampleTwo());
simpleUrlHandlerMapping.setUrlMap(urlMap); simpleUrlHandlerMapping.setUrlMap(urlMap);
return simpleUrlHandlerMapping; return simpleUrlHandlerMapping;
} }
@Bean @Bean
public WelcomeController welcome() { public WelcomeController welcome() {
WelcomeController welcome = new WelcomeController(); return new WelcomeController();
return welcome;
} }
@Bean
public ExampleTwoController exampleTwo() {
ExampleTwoController exampleTwo = new ExampleTwoController();
return exampleTwo;
}
} }

View File

@ -1,18 +1,15 @@
package com.baeldung.controller; package com.baeldung.web.controller.handlermapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController; import org.springframework.web.servlet.mvc.AbstractController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class BaeldungController extends AbstractController { public class BaeldungController extends AbstractController {
@Override @Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Inside Welcome Baeldung Controller");
ModelAndView model = new ModelAndView("baeldung"); ModelAndView model = new ModelAndView("baeldung");
return model; return model;
} }

View File

@ -1,22 +0,0 @@
package com.baeldung.web.controller.handlermapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
public class ExampleTwoController extends AbstractController
{
@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
HttpServletResponse response) throws Exception {
System.out.println("Inside ExampleTwo Controller");
ModelAndView model = new ModelAndView("exampleTwo");
return model;
}
}

View File

@ -1,18 +1,15 @@
package com.baeldung.controller; package com.baeldung.web.controller.handlermapping;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView; import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController; import org.springframework.web.servlet.mvc.AbstractController;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TestController extends AbstractController { public class TestController extends AbstractController {
@Override @Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception { protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
System.out.println("Inside Welcome two Controller");
ModelAndView model = new ModelAndView("test"); ModelAndView model = new ModelAndView("test");
return model; return model;
} }

View File

@ -8,15 +8,10 @@ import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
@Controller @Controller
public class WelcomeController extends AbstractController public class WelcomeController extends AbstractController {
{
@Override @Override
protected ModelAndView handleRequestInternal(HttpServletRequest request, protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response) throws Exception {
HttpServletResponse response) throws Exception { return new ModelAndView("welcome");
System.out.println("Inside BeanNameMappingExampleOne Controller");
ModelAndView model = new ModelAndView("test");
return model;
} }
} }

View File

@ -1,10 +1,6 @@
package come.baeldung.test; package com.baeldung.handlermapping;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import com.baeldung.spring.web.config.BeanNameUrlHandlerMappingConfig;
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;
@ -17,12 +13,15 @@ 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 com.baeldung.config.WebAppConfiguration_BeanNameUrlHandlerMapping; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
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)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = WebAppConfiguration_BeanNameUrlHandlerMapping.class) @ContextConfiguration(classes = BeanNameUrlHandlerMappingConfig.class)
public class BeanNameMappingConfigTest { public class BeanNameMappingConfigTest {
@Autowired @Autowired

View File

@ -1,4 +1,4 @@
package come.baeldung.test; package com.baeldung.handlermapping;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
@ -17,11 +17,11 @@ 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 com.baeldung.config.WebAppConfiguration_ControllerClassNameHandlerMapping; import com.baeldung.spring.web.config.ControllerClassNameHandlerMappingConfig;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = WebAppConfiguration_ControllerClassNameHandlerMapping.class) @ContextConfiguration(classes = ControllerClassNameHandlerMappingConfig.class)
public class ControllerClassNameHandlerMappingTest { public class ControllerClassNameHandlerMappingTest {
@Autowired @Autowired
@ -36,6 +36,6 @@ public class ControllerClassNameHandlerMappingTest {
@Test @Test
public void whenControllerClassNameMapping_thenMappedOK() throws Exception { public void whenControllerClassNameMapping_thenMappedOK() throws Exception {
mockMvc.perform(get("/welcometest")).andExpect(status().isOk()).andExpect(view().name("welcome")).andDo(print()); mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("welcome")).andDo(print());
} }
} }

View File

@ -1,4 +1,4 @@
package come.baeldung.test; package com.baeldung.handlermapping;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print; import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
@ -17,11 +17,11 @@ 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 com.baeldung.config.WebAppConfiguration_ConfiguringPriorities; import com.baeldung.spring.web.config.HandlerMappingPrioritiesConfig;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = WebAppConfiguration_ConfiguringPriorities.class) @ContextConfiguration(classes = HandlerMappingPrioritiesConfig.class)
public class HandlerMappingPriorityConfigTest { public class HandlerMappingPriorityConfigTest {
@Autowired @Autowired

View File

@ -1,10 +1,6 @@
package come.baeldung.test; package com.baeldung.handlermapping;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
import com.baeldung.spring.web.config.SimpleUrlHandlerMappingConfig;
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;
@ -17,11 +13,14 @@ 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 com.baeldung.config.WebAppConfiguration_SimpleUrlHandlerMapping; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
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)
@WebAppConfiguration @WebAppConfiguration
@ContextConfiguration(classes = WebAppConfiguration_SimpleUrlHandlerMapping.class) @ContextConfiguration(classes = SimpleUrlHandlerMappingConfig.class)
public class SimpleUrlMappingConfigTest { public class SimpleUrlMappingConfigTest {
@Autowired @Autowired

View File

@ -1,41 +0,0 @@
package com.baeldung.web.controller;
import com.baeldung.spring.web.config.BeanNameHandlerMappingConfig;
import com.baeldung.spring.web.config.SimpleUrlHandlerMappingConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = BeanNameHandlerMappingConfig.class)
public class BeanNameMappingConfigTest {
@Autowired
private WebApplicationContext webAppContext;
private MockMvc mockMvc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
}
@Test
public void whenBeanNameMapping_thenMappedOK() throws Exception {
mockMvc.perform(get("/welcomeBean")).andExpect(status().isOk()).andExpect(view().name("test")).andDo(print());
}
}

View File

@ -1,39 +0,0 @@
package com.baeldung.web.controller;
import com.baeldung.spring.web.config.ControllerClassNameHandlerMappingConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = ControllerClassNameHandlerMappingConfig.class)
public class ControllerClassNameHandlerMappingTest {
@Autowired
private WebApplicationContext webAppContext;
private MockMvc mockMvc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
}
@Test
public void whenControllerClassNameMapping_thenMappedOK() throws Exception {
mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("test")).andDo(print());
}
}

View File

@ -1,40 +0,0 @@
package com.baeldung.web.controller;
import com.baeldung.spring.web.config.SimpleUrlHandlerMappingConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
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 static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.view;
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration(classes = SimpleUrlHandlerMappingConfig.class)
public class SimpleUrlMappingConfigTest {
@Autowired
private WebApplicationContext webAppContext;
private MockMvc mockMvc;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
}
@Test
public void whenSimpleUrlMapping_thenMappedOK() throws Exception {
mockMvc.perform(get("/simpleUrlWelcome")).andExpect(status().isOk()).andExpect(view().name("test")).andDo(print());
}
}