JAVA-3519: Moved spring-mvc-basics-2 inside spring-web-modules
This commit is contained in:
+22
@@ -0,0 +1,22 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||
import org.springframework.test.context.web.WebAppConfiguration;
|
||||
|
||||
import com.baeldung.spring.configuration.ApplicationConfiguration;
|
||||
import com.baeldung.spring.configuration.EmailConfiguration;
|
||||
|
||||
|
||||
@RunWith(SpringJUnit4ClassRunner.class)
|
||||
@ContextConfiguration(classes={ApplicationConfiguration.class, EmailConfiguration.class})
|
||||
@WebAppConfiguration
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
package com.baeldung.controller.push;
|
||||
|
||||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
|
||||
import org.springframework.test.web.servlet.MockMvc;
|
||||
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
|
||||
import org.springframework.web.context.WebApplicationContext;
|
||||
|
||||
import com.baeldung.spring.configuration.PushConfiguration;
|
||||
|
||||
@SpringJUnitWebConfig(PushConfiguration.class)
|
||||
public class PushControllerIntegrationTest {
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDemoWithPushGETisPerformed_thenRetrievedStatusOk() throws Exception {
|
||||
mockMvc.perform(get("/demoWithPush"))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDemoWithoutPushGETisPerformed_thenRetrievedStatusOk() throws Exception {
|
||||
mockMvc.perform(get("/demoWithoutPush"))
|
||||
.andExpect(status().isOk());
|
||||
}
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
package com.baeldung.controller.rss;
|
||||
|
||||
import com.baeldung.spring.configuration.ApplicationConfiguration;
|
||||
import com.baeldung.spring.configuration.EmailConfiguration;
|
||||
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.test.context.junit.jupiter.web.SpringJUnitWebConfig;
|
||||
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.MockMvcResultMatchers.content;
|
||||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
|
||||
|
||||
@SpringJUnitWebConfig(classes={ApplicationConfiguration.class, EmailConfiguration.class})
|
||||
public class ArticleRssIntegrationTest {
|
||||
public static final String APPLICATION_RSS_XML = "application/rss+xml";
|
||||
public static final String APPLICATION_RSS_JSON = "application/rss+json";
|
||||
public static final String APPLICATION_RSS_XML_CHARSET_UTF_8 = "application/rss+xml;charset=UTF-8";
|
||||
|
||||
@Autowired
|
||||
private WebApplicationContext webAppContext;
|
||||
private MockMvc mockMvc;
|
||||
|
||||
@BeforeEach
|
||||
public void setup() {
|
||||
mockMvc = MockMvcBuilders.webAppContextSetup(webAppContext)
|
||||
.build();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingXMLFeed_thenContentTypeIsOk() throws Exception {
|
||||
mockMvc.perform(get("/rss2").accept(APPLICATION_RSS_XML))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(APPLICATION_RSS_XML_CHARSET_UTF_8));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenRequestingJSONFeed_thenContentTypeIsOk() throws Exception {
|
||||
mockMvc.perform(get("/rss2").accept(APPLICATION_RSS_JSON))
|
||||
.andExpect(status().isOk())
|
||||
.andExpect(content().contentType(APPLICATION_RSS_JSON));
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.spring.servlets;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class HelloServletIntegrationTest {
|
||||
@Test
|
||||
public void whenRequested_thenForwardToCorrectUrl() throws ServletException, IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/hello");
|
||||
request.addParameter("name", "Dennis");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
HelloServlet servlet = new HelloServlet();
|
||||
|
||||
servlet.doGet(request, response);
|
||||
|
||||
assertEquals("/forwarded", response.getForwardedUrl());
|
||||
assertEquals(200, response.getStatus());
|
||||
}
|
||||
}
|
||||
+24
@@ -0,0 +1,24 @@
|
||||
package com.baeldung.spring.servlets;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
import org.springframework.mock.web.MockHttpServletResponse;
|
||||
import javax.servlet.ServletException;
|
||||
import java.io.IOException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
|
||||
public class WelcomeServletIntegrationTest {
|
||||
@Test
|
||||
public void whenRequested_thenRedirectedToCorrectUrl() throws ServletException, IOException {
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("GET", "/welcome");
|
||||
request.addParameter("name", "Dennis");
|
||||
WelcomeServlet servlet = new WelcomeServlet();
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
|
||||
servlet.doGet(request, response);
|
||||
|
||||
assertEquals("/redirected", response.getRedirectedUrl());
|
||||
assertEquals(302, response.getStatus());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user