Merge branch 'master' into pr/949-yasser-handler-mapping
This commit is contained in:
@@ -1,2 +0,0 @@
|
||||
### Relevant Articles:
|
||||
- [Circular Dependencies in Spring](http://www.baeldung.com/circular-dependencies-in-spring)
|
||||
@@ -0,0 +1,41 @@
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,42 @@
|
||||
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.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.web.servlet.ViewResolver;
|
||||
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||
import org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
@Configuration
|
||||
public class ControllerClassNameHandlerMappingConfig {
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ExampleTwoController exampleTwo() {
|
||||
ExampleTwoController exampleTwo = new ExampleTwoController();
|
||||
return exampleTwo;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
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.SimpleUrlHandlerMapping;
|
||||
import org.springframework.web.servlet.view.InternalResourceViewResolver;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
public class SimpleUrlHandlerMappingConfig {
|
||||
|
||||
@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());
|
||||
urlMap.put("/exampleTwo", exampleTwo());
|
||||
simpleUrlHandlerMapping.setUrlMap(urlMap);
|
||||
return simpleUrlHandlerMapping;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public WelcomeController welcome() {
|
||||
WelcomeController welcome = new WelcomeController();
|
||||
return welcome;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public ExampleTwoController exampleTwo() {
|
||||
ExampleTwoController exampleTwo = new ExampleTwoController();
|
||||
return exampleTwo;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
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 WelcomeController extends AbstractController
|
||||
{
|
||||
@Override
|
||||
protected ModelAndView handleRequestInternal(HttpServletRequest request,
|
||||
HttpServletResponse response) throws Exception {
|
||||
System.out.println("Inside BeanNameMappingExampleOne Controller");
|
||||
|
||||
ModelAndView model = new ModelAndView("test");
|
||||
|
||||
return model;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
<!DOCTYPE html>
|
||||
<html xmlns="http://www.w3.org/1999/xhtml"
|
||||
xmlns:th="http://www.thymeleaf.org">
|
||||
<head>
|
||||
<meta charset="UTF-8" />
|
||||
</head>
|
||||
<body>
|
||||
<form action="#" th:action="@{/message/processForm}" method="post">
|
||||
Message: <input type="text" value="message" id="message" name="message"/>
|
||||
<input type="submit" />
|
||||
</form>
|
||||
|
||||
<span th:text="${message}" id="received"></span>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,2 +0,0 @@
|
||||
### Relevant Articles:
|
||||
- [Circular Dependencies in Spring](http://www.baeldung.com/circular-dependencies-in-spring)
|
||||
@@ -0,0 +1,32 @@
|
||||
package com.baeldung.htmlunit;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
|
||||
public class HtmlUnitAndJUnitLiveTest {
|
||||
|
||||
private WebClient webClient;
|
||||
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
webClient = new WebClient();
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() throws Exception {
|
||||
webClient.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsOk() throws Exception {
|
||||
webClient.getOptions().setThrowExceptionOnScriptError(false);
|
||||
HtmlPage page = webClient.getPage("http://www.baeldung.com/");
|
||||
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,72 +0,0 @@
|
||||
package com.baeldung.htmlunit;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Ignore;
|
||||
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.htmlunit.MockMvcWebClientBuilder;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException;
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { TestConfig.class })
|
||||
public class HtmlUnitAndSpringIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private WebClient webClient;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
webClient = MockMvcWebClientBuilder.webAppContextSetup(wac).build();
|
||||
}
|
||||
|
||||
//
|
||||
|
||||
@Test
|
||||
@Ignore("Related view message.html does not exist check MessageController")
|
||||
public void givenAMessage_whenSent_thenItShows() throws FailingHttpStatusCodeException, MalformedURLException, IOException {
|
||||
final String text = "Hello world!";
|
||||
final HtmlPage page = webClient.getPage("http://localhost/message/showForm");
|
||||
System.out.println(page.asXml());
|
||||
|
||||
final HtmlTextInput messageText = page.getHtmlElementById("message");
|
||||
messageText.setValueAttribute(text);
|
||||
|
||||
final HtmlForm form = page.getForms().get(0);
|
||||
final HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");
|
||||
final HtmlPage newPage = submit.click();
|
||||
|
||||
final String receivedText = newPage.getHtmlElementById("received").getTextContent();
|
||||
|
||||
Assert.assertEquals(receivedText, text);
|
||||
System.out.println(newPage.asXml());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
|
||||
try (final WebClient client = new WebClient()) {
|
||||
webClient.getOptions().setThrowExceptionOnScriptError(false);
|
||||
|
||||
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
|
||||
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
package com.baeldung.htmlunit;
|
||||
|
||||
import org.junit.Assert;
|
||||
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.htmlunit.MockMvcWebClientBuilder;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlForm;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlSubmitInput;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlTextInput;
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@WebAppConfiguration
|
||||
@ContextConfiguration(classes = { TestConfig.class })
|
||||
public class HtmlUnitAndSpringLiveTest {
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext wac;
|
||||
|
||||
private WebClient webClient;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
webClient = MockMvcWebClientBuilder.webAppContextSetup(wac).build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenAMessage_whenSent_thenItShows() throws Exception {
|
||||
String text = "Hello world!";
|
||||
HtmlPage page;
|
||||
|
||||
String url = "http://localhost/message/showForm";
|
||||
page = webClient.getPage(url);
|
||||
|
||||
HtmlTextInput messageText = page.getHtmlElementById("message");
|
||||
messageText.setValueAttribute(text);
|
||||
|
||||
HtmlForm form = page.getForms().get(0);
|
||||
HtmlSubmitInput submit = form.getOneHtmlElementByAttribute("input", "type", "submit");
|
||||
HtmlPage newPage = submit.click();
|
||||
|
||||
String receivedText = newPage.getHtmlElementById("received").getTextContent();
|
||||
|
||||
Assert.assertEquals(receivedText, text);
|
||||
}
|
||||
}
|
||||
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.htmlunit;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
|
||||
public class HtmlUnitTest {
|
||||
|
||||
@Test
|
||||
public void givenAClient_whenEnteringBaeldung_thenPageTitleIsCorrect() throws Exception {
|
||||
try (final WebClient webClient = new WebClient()) {
|
||||
webClient.getOptions().setThrowExceptionOnScriptError(false);
|
||||
|
||||
final HtmlPage page = webClient.getPage("http://www.baeldung.com/");
|
||||
Assert.assertEquals("Baeldung | Java, Spring and Web Development tutorials", page.getTitleText());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
package com.baeldung.htmlunit;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
|
||||
public class HtmlUnitWebLiveScraping {
|
||||
|
||||
private WebClient webClient;
|
||||
|
||||
@Before
|
||||
public void init() throws Exception {
|
||||
webClient = new WebClient();
|
||||
}
|
||||
|
||||
@After
|
||||
public void close() throws Exception {
|
||||
webClient.close();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void givenBaeldungArchive_whenRetrievingArticle_thenHasH1() throws Exception {
|
||||
webClient.getOptions().setCssEnabled(false);
|
||||
webClient.getOptions().setJavaScriptEnabled(false);
|
||||
|
||||
final String url = "http://www.baeldung.com/full_archive";
|
||||
final HtmlPage page = webClient.getPage(url);
|
||||
final String xpath = "(//ul[@class='car-monthlisting']/li)[1]/a";
|
||||
final HtmlAnchor latestPostLink = (HtmlAnchor) page.getByXPath(xpath).get(0);
|
||||
final HtmlPage postPage = latestPostLink.click();
|
||||
|
||||
final List<HtmlHeading1> h1 = (List<HtmlHeading1>) postPage.getByXPath("//h1");
|
||||
|
||||
Assert.assertTrue(h1.size() > 0);
|
||||
}
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.baeldung.htmlunit;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.gargoylesoftware.htmlunit.WebClient;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlAnchor;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlHeading1;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlHeading2;
|
||||
import com.gargoylesoftware.htmlunit.html.HtmlPage;
|
||||
|
||||
public class HtmlUnitWebScraping {
|
||||
|
||||
public static void main(final String[] args) throws Exception {
|
||||
try (final WebClient webClient = new WebClient()) {
|
||||
|
||||
webClient.getOptions().setCssEnabled(false);
|
||||
webClient.getOptions().setJavaScriptEnabled(false);
|
||||
|
||||
final HtmlPage page = webClient.getPage("http://www.baeldung.com/full_archive");
|
||||
final HtmlAnchor latestPostLink = (HtmlAnchor) page.getByXPath("(//ul[@class='car-monthlisting']/li)[1]/a").get(0);
|
||||
|
||||
System.out.println("Entering: " + latestPostLink.getHrefAttribute());
|
||||
|
||||
final HtmlPage postPage = latestPostLink.click();
|
||||
|
||||
final HtmlHeading1 heading1 = (HtmlHeading1) postPage.getByXPath("//h1").get(0);
|
||||
System.out.println("Title: " + heading1.getTextContent());
|
||||
|
||||
final List<HtmlHeading2> headings2 = (List<HtmlHeading2>) postPage.getByXPath("//h2");
|
||||
|
||||
final StringBuilder sb = new StringBuilder(heading1.getTextContent());
|
||||
for (final HtmlHeading2 h2 : headings2) {
|
||||
sb.append("\n").append(h2.getTextContent());
|
||||
}
|
||||
|
||||
System.out.println(sb.toString());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
@@ -32,7 +32,7 @@ public class GreetControllerIntegrationTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
|
||||
private static final String CONTENT_TYPE = "application/json";
|
||||
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
|
||||
|
||||
@Before
|
||||
public void setup() throws Exception {
|
||||
@@ -41,7 +41,7 @@ public class GreetControllerIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenWAC_whenServletContext_thenItProvidesGreetController() {
|
||||
ServletContext servletContext = wac.getServletContext();
|
||||
final ServletContext servletContext = wac.getServletContext();
|
||||
Assert.assertNotNull(servletContext);
|
||||
Assert.assertTrue(servletContext instanceof MockServletContext);
|
||||
Assert.assertNotNull(wac.getBean("greetController"));
|
||||
@@ -54,7 +54,7 @@ public class GreetControllerIntegrationTest {
|
||||
|
||||
@Test
|
||||
public void givenGreetURI_whenMockMVC_thenVerifyResponse() throws Exception {
|
||||
MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/greet")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")).andReturn();
|
||||
final MvcResult mvcResult = this.mockMvc.perform(MockMvcRequestBuilders.get("/greet")).andDo(print()).andExpect(MockMvcResultMatchers.status().isOk()).andExpect(MockMvcResultMatchers.jsonPath("$.message").value("Hello World!!!")).andReturn();
|
||||
Assert.assertEquals(CONTENT_TYPE, mvcResult.getResponse().getContentType());
|
||||
}
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@ import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
public class GreetControllerUnitTest {
|
||||
|
||||
private MockMvc mockMvc;
|
||||
private static final String CONTENT_TYPE = "application/json";
|
||||
private static final String CONTENT_TYPE = "application/json;charset=UTF-8";
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
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());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user