BAEL-97 - simplifying code
This commit is contained in:
parent
4989946b09
commit
49cf1e6ed8
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.config;
|
||||
|
||||
import com.baeldung.web.controller.handlermapping.BeanNameHandlerMappingController;
|
||||
import com.baeldung.web.controller.handlermapping.SimpleUrlMappingController;
|
||||
import com.baeldung.web.controller.handlermapping.WelcomeController;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
|
||||
@Configuration
|
||||
public class HandlerMappingDefaultConfig {
|
||||
|
||||
@Bean("/welcome")
|
||||
public BeanNameHandlerMappingController beanNameHandlerMapping() {
|
||||
return new BeanNameHandlerMappingController();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WelcomeController welcome() {
|
||||
return new WelcomeController();
|
||||
}
|
||||
|
||||
}
|
|
@ -18,13 +18,14 @@ public class HandlerMappingPrioritiesConfig {
|
|||
@Bean
|
||||
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping() {
|
||||
BeanNameUrlHandlerMapping beanNameUrlHandlerMapping = new BeanNameUrlHandlerMapping();
|
||||
beanNameUrlHandlerMapping.setOrder(0);
|
||||
beanNameUrlHandlerMapping.setOrder(1);
|
||||
return beanNameUrlHandlerMapping;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
|
||||
SimpleUrlHandlerMapping simpleUrlHandlerMapping = new SimpleUrlHandlerMapping();
|
||||
simpleUrlHandlerMapping.setOrder(0);
|
||||
Map<String, Object> urlMap = new HashMap<>();
|
||||
urlMap.put("/welcome", simpleUrlMapping());
|
||||
simpleUrlHandlerMapping.setUrlMap(urlMap);
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package com.baeldung.handlermappings;
|
||||
|
||||
import com.baeldung.config.HandlerMappingDefaultConfig;
|
||||
import com.baeldung.config.HandlerMappingPrioritiesConfig;
|
||||
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 = HandlerMappingDefaultConfig.class)
|
||||
public class HandlerMappingDefaultConfigTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDefaultConfig_thenMappedOK() throws Exception {
|
||||
mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("bean-name-handler-mapping")).andDo(print());
|
||||
}
|
||||
}
|
|
@ -35,6 +35,6 @@ public class HandlerMappingPriorityConfigTest {
|
|||
|
||||
@Test
|
||||
public void whenConfiguringPriorities_thenMappedOK() throws Exception {
|
||||
mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("bean-name-handler-mapping")).andDo(print());
|
||||
mockMvc.perform(get("/welcome")).andExpect(status().isOk()).andExpect(view().name("simple-url-handler-mapping")).andDo(print());
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue