Search in sources :

Example 46 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project cucumber-jvm by cucumber.

the class SpringInjectionStepDefs method I_call_the_url.

@When("^I call the url \"([^\"]*)\"$")
public void I_call_the_url(String url) throws Throwable {
    MockMvc mock = MockMvcBuilders.webAppContextSetup(wac).build();
    callUrl = mock.perform(get(url));
}
Also used : MockMvc(org.springframework.test.web.servlet.MockMvc) When(cucumber.api.java.en.When)

Example 47 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-framework by spring-projects.

the class SharedHttpSessionTests method noHttpSession.

@Test
public void noHttpSession() throws Exception {
    MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new TestController()).apply(sharedHttpSession()).build();
    String url = "/no-session";
    MvcResult result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn();
    HttpSession session = result.getRequest().getSession(false);
    assertNull(session);
    result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn();
    session = result.getRequest().getSession(false);
    assertNull(session);
    url = "/session";
    result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn();
    session = result.getRequest().getSession(false);
    assertNotNull(session);
    assertEquals(1, session.getAttribute("counter"));
}
Also used : HttpSession(javax.servlet.http.HttpSession) SharedHttpSessionConfigurer.sharedHttpSession(org.springframework.test.web.servlet.setup.SharedHttpSessionConfigurer.sharedHttpSession) MvcResult(org.springframework.test.web.servlet.MvcResult) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 48 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-framework by spring-projects.

the class SharedHttpSessionTests method httpSession.

@Test
public void httpSession() throws Exception {
    MockMvc mockMvc = MockMvcBuilders.standaloneSetup(new TestController()).apply(sharedHttpSession()).build();
    String url = "/session";
    MvcResult result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn();
    HttpSession session = result.getRequest().getSession(false);
    assertNotNull(session);
    assertEquals(1, session.getAttribute("counter"));
    result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn();
    session = result.getRequest().getSession(false);
    assertNotNull(session);
    assertEquals(2, session.getAttribute("counter"));
    result = mockMvc.perform(get(url)).andExpect(status().isOk()).andReturn();
    session = result.getRequest().getSession(false);
    assertNotNull(session);
    assertEquals(3, session.getAttribute("counter"));
}
Also used : HttpSession(javax.servlet.http.HttpSession) SharedHttpSessionConfigurer.sharedHttpSession(org.springframework.test.web.servlet.setup.SharedHttpSessionConfigurer.sharedHttpSession) MvcResult(org.springframework.test.web.servlet.MvcResult) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 49 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-framework by spring-projects.

the class DefaultMockMvcBuilderTests method dispatcherServletCustomizerProcessedInOrder.

@Test
public void dispatcherServletCustomizerProcessedInOrder() {
    StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext);
    DefaultMockMvcBuilder builder = webAppContextSetup(root);
    builder.addDispatcherServletCustomizer(ds -> ds.setContextId("test-id"));
    builder.addDispatcherServletCustomizer(ds -> ds.setContextId("override-id"));
    builder.dispatchOptions(true);
    MockMvc mvc = builder.build();
    DispatcherServlet ds = (DispatcherServlet) new DirectFieldAccessor(mvc).getPropertyValue("servlet");
    assertEquals("override-id", ds.getContextId());
}
Also used : DispatcherServlet(org.springframework.web.servlet.DispatcherServlet) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Example 50 with MockMvc

use of org.springframework.test.web.servlet.MockMvc in project spring-framework by spring-projects.

the class MockMvcConnectionBuilderSupportTests method mockMvc.

@Test
public void mockMvc() throws Exception {
    MockMvc mockMvc = MockMvcBuilders.webAppContextSetup(wac).build();
    WebConnection conn = new MockMvcWebConnectionBuilderSupport(mockMvc) {
    }.createConnection(this.client);
    assertMockMvcUsed(conn, "http://localhost/");
    assertMockMvcNotUsed(conn, "http://example.com/");
}
Also used : WebConnection(com.gargoylesoftware.htmlunit.WebConnection) MockMvc(org.springframework.test.web.servlet.MockMvc) Test(org.junit.Test)

Aggregations

MockMvc (org.springframework.test.web.servlet.MockMvc)97 Test (org.junit.Test)91 AnnotationConfigWebApplicationContext (org.springframework.web.context.support.AnnotationConfigWebApplicationContext)25 Todo (org.springframework.sync.Todo)16 List (java.util.List)15 TodoRepository (org.springframework.sync.TodoRepository)15 Filter (javax.servlet.Filter)13 MockServletContext (org.springframework.mock.web.MockServletContext)13 AnnotationConfigApplicationContext (org.springframework.context.annotation.AnnotationConfigApplicationContext)12 OncePerRequestFilter (org.springframework.web.filter.OncePerRequestFilter)9 MvcResult (org.springframework.test.web.servlet.MvcResult)6 TestingAuthenticationToken (org.springframework.security.authentication.TestingAuthenticationToken)5 Ignore (org.junit.Ignore)3 FilterChainProxy (org.springframework.security.web.FilterChainProxy)3 WebConnection (com.gargoylesoftware.htmlunit.WebConnection)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 HttpSession (javax.servlet.http.HttpSession)2 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)2 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)2 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)2