Spring Framework MockMvc - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Spring Framework MockMvc class.

This code example shows how to use the following methods:perform
	} 
 
	@Test
	public void test() throws Exception {
		this.servletRequest.setAttribute("foo1", "bar");
		this.mockMvc.perform(get("/myUrl").requestAttr("foo2", "bar")).andExpect(status().isOk());
	} 
 
 
	@Configuration 
	@EnableWebMvc 
	static class WebConfig extends WebMvcConfigurerAdapter {
 
		@Bean 
		public MyController myController() { 
			return new MyController(); 
		} 
	} 
 
	@Controller 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:perform
		this.mockMvc = standaloneSetup(this.personController).build(); 
	} 
 
	@Test 
	public void stringWithCorrectResponseHeaderValue() throws Exception { 
		this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, currentTime - (1000 * 60)))// 
		.andExpect(header().string(LAST_MODIFIED, String.valueOf(currentTime))); 
	} 
 
	@Test 
	public void stringWithMatcherAndCorrectResponseHeaderValue() throws Exception { 
		this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, currentTime - (1000 * 60)))// 
		.andExpect(header().string(LAST_MODIFIED, equalTo(String.valueOf(currentTime)))); 
	} 
 
	@Test 
	public void longValueWithCorrectResponseHeaderValue() throws Exception { 
		this.mockMvc.perform(get("/persons/1").header(IF_MODIFIED_SINCE, currentTime - (1000 * 60)))// 
		.andExpect(header().longValue(LAST_MODIFIED, currentTime)); 
	} 
6
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:perform
    } 
 
    @Test
    public void requestProtectedUrlWithAdmin() throws Exception {
        mvc
            .perform(get("/admin").with(user("admin").roles("ADMIN")))
            // Ensure we got past Security 
            .andExpect(status().isNotFound())
            // Ensure it appears we are authenticated with admin 
            .andExpect(authenticated().withUsername("admin")); 
    } 
 
    @Test
    public void requestProtectedUrlWithUserDetails() throws Exception {
        UserDetails user = userDetailsService.loadUserByUsername("user");
        mvc
            .perform(get("/").with(user(user)))
            // Ensure we got past Security 
            .andExpect(status().isNotFound())
            // Ensure it appears we are authenticated with user 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:perform
        mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); 
    } 
 
    @Test 
    public void testDisplayForm() throws Exception { 
        mockMvc.perform(get("/signup.html")) 
            .andExpect(status().isOk()) 
            .andExpect(model().attributeExists("user")); 
    } 
 
    @Test 
    public void testSignupUser() throws Exception { 
        // start SMTP Server 
        Wiser wiser = new Wiser(); 
        wiser.setPort(getSmtpPort()); 
        wiser.start(); 
 
        ResultActions signup = mockMvc.perform(post("/signup.html") 
                .param("address.city", "Denver") 
                .param("address.province", "Colorado") 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:perform
        MockHttpServletRequestBuilder composeMessage = 
            post("/") 
                .param("summary", "New Message") 
                .param("text", "This is a new message"); 
 
        mvc.perform(composeMessage) 
            .andExpect(status().isForbidden()); 
    } 
 
    @Test 
    @WithMockUser 
    public void composeMessage() throws Exception { 
        MockHttpServletRequestBuilder composeMessage = 
            post("/") 
                .param("summary", "New Message") 
                .param("text", "This is a new message") 
                .with(csrf()); 
 
        mvc.perform(composeMessage) 
            .andExpect(redirectedUrlPattern("/*")); 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:perform
	@Test 
	public void testDto() throws Exception { 
 
		String jsonContent = "{\"id\":1,\"value1\":\"foo\",\"value2\":\"bar\"}"; 
 
		this.mockMvc.perform(post("/testcontroller/test") 
				.content(jsonContent)) 
				.andExpect(status().isOk()) 
				.andExpect(content().string(jsonContent)); 
	} 
 
	@Test 
	public void testDtoLists() throws Exception { 
 
		String jsonContent = 
				"[{\"id\":1,\"value1\":\"foo1\",\"value2\":\"bar1\"}," + 
				"{\"id\":2,\"value1\":\"foo2\",\"value2\":\"bar2\"}," + 
				"{\"id\":3,\"value1\":\"foo3\",\"value2\":\"bar3\"}]"; 
 
		this.mockMvc.perform(post("/testcontroller/testList") 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:perform
	@Test 
	public void testDto() throws Exception { 
 
		String jsonContent = "{\"id\":1,\"value1\":\"foo\",\"value2\":\"bar\"}"; 
 
		this.mockMvc.perform(post("/testcontroller2/test2") 
				.content(jsonContent)) 
				.andExpect(status().isOk()) 
				.andExpect(content().string(jsonContent)); 
	} 
 
	@Test 
	public void testDtoLists() throws Exception { 
 
		String jsonContent = 
				"[{\"id\":1,\"value1\":\"foo1\",\"value2\":\"bar1\"}," + 
				"{\"id\":2,\"value1\":\"foo2\",\"value2\":\"bar2\"}," + 
				"{\"id\":3,\"value1\":\"foo3\",\"value2\":\"bar3\"}]"; 
 
		this.mockMvc.perform(post("/testcontroller2/test2List") 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:perform
        Assert.assertTrue(locationUri.contains("/users/" + userId + "/customers/")); 
    } 
 
    @Test 
    public void testLoadingACustomerThatDoesNotExist() throws Exception { 
        this.mockMvc.perform(get("/users/" + 5 + "/customers/" + 24022) 
                .accept(this.applicationJsonMediaType)) 
                .andExpect(status().isNotFound()) 
                .andExpect(content().contentType(this.vndErrorMediaType)); 
 
    } 
 
    @Test 
    public void testLoadingAUserThatDoesNotExist() throws Exception { 
        this.mockMvc.perform(get("/users/" + 400) 
                .accept(this.applicationJsonMediaType)) 
                .andExpect(status().isNotFound()) 
                .andExpect(content().contentType(this.vndErrorMediaType)); 
 
    } 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:perform
        Assert.assertTrue(locationUri.contains("/users/" + userId + "/customers/")); 
    } 
 
    @Test 
    public void testLoadingACustomerThatDoesNotExist() throws Exception { 
        this.mockMvc.perform(get("/users/" + 5 + "/customers/" + 24022) 
                .accept(this.applicationJsonMediaType)) 
                .andExpect(status().isNotFound()) 
                .andExpect(content().contentType(this.vndErrorMediaType)); 
 
    } 
 
    @Test 
    public void testLoadingAUserThatDoesNotExist() throws Exception { 
        this.mockMvc.perform(get("/users/" + 400) 
                .accept(this.applicationJsonMediaType)) 
                .andExpect(status().isNotFound()) 
                .andExpect(content().contentType(this.vndErrorMediaType)); 
 
    } 
5
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
This code example shows how to use the following methods:perform
 
    @Test 
    public void creationOfTGTWithAuthenticationException() throws Throwable { 
        configureCasMockTGTCreationToThrowAuthenticationException(); 
 
        this.mockMvc.perform(post("/cas/v1/tickets") 
                .param("username", "test") 
                .param("password", "test")) 
                .andExpect(status().is4xxClientError()) 
                .andExpect(content().string("1 errors, 0 successes")); 
    } 
 
    @Test 
    public void normalCreationOfST() throws Throwable { 
        configureCasMockToCreateValidST(); 
 
        this.mockMvc.perform(post("/cas/v1/tickets/TGT-1") 
                .param("service", "https://www.google.com")) 
                .andExpect(status().isOk()) 
                .andExpect(content().contentType("text/plain;charset=ISO-8859-1")) 
4
CodeRank
Contextual code suggestions in your IDE  Get Codota for Java
See Code Examples for Spring Framework MockMvc Methods: