Examples of MockMvc


Examples of org.springframework.test.web.server.MockMvc

    ContentNegotiatingViewResolver cnViewResolver = new ContentNegotiatingViewResolver();
    cnViewResolver.setDefaultViews(viewList);
    cnViewResolver.setDefaultContentType(MediaType.TEXT_HTML);

    MockMvc mockMvc =
      standaloneSetup(new PersonController())
        .setViewResolvers(cnViewResolver, new InternalResourceViewResolver())
        .build();

    mockMvc.perform(get("/person/Corea"))
      .andExpect(status().isOk())
      .andExpect(model().size(1))
      .andExpect(model().attributeExists("person"))
      .andExpect(forwardedUrl("person/show"));

    mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_JSON))
      .andExpect(status().isOk())
      .andExpect(content().mimeType(MediaType.APPLICATION_JSON))
      .andExpect(jsonPath("$.person.name").value("Corea"));

    mockMvc.perform(get("/person/Corea").accept(MediaType.APPLICATION_XML))
      .andExpect(status().isOk())
      .andExpect(content().mimeType(MediaType.APPLICATION_XML))
      .andExpect(xpath("/person/name/text()").string(equalTo("Corea")));
  }
View Full Code Here

Examples of org.springframework.test.web.server.MockMvc

        Mockito.when(this.accountService.login("john", "secret")).thenReturn(this.account);
    }

    @Test
    public void testHandleLogin() throws Exception {
        MockMvc mockMvc = MockMvcBuilders.standaloneSetup(this.loginController).build();
        mockMvc.perform(post("/login").param("username", "john").param("password", "secret"))
                .andExpect(status().isOk())
                .andExpect(request().sessionAttribute(LoginController.ACCOUNT_ATTRIBUTE, this.account))
                .andExpect(redirectedUrl("/index.htm"));
    }
View Full Code Here

Examples of org.springframework.test.web.server.MockMvc

    AccountRepository accountRepository = mock(AccountRepository.class);   
    SignedUpGateway gateway = mock(SignedUpGateway.class);   
    SignupController signupController = new SignupController(accountRepository, gateway);
   
    String signupJson = "{\"first-name\":\"Roy\",\"last-name\":\"Clarkson\",\"email\":\"roy@clarkson.com\",\"confirm-email\":\"roy@clarkson.com\",\"gender\":\"M\",\"birthdate\":{\"month\":7,\"day\":8,\"year\":1976},\"password\":\"letmein\"}";
    MockMvc mockMvc = standaloneSetup(signupController).build();
    mockMvc.perform(post("/signup").contentType(APPLICATION_JSON).body(signupJson.getBytes()))
        .andExpect(MockMvcResultMatchers.status().isCreated())
        .andExpect(MockMvcResultMatchers.jsonPath("message", Matchers.equalTo("Account created")));
  }
View Full Code Here

Examples of org.springframework.test.web.server.MockMvc

    when(accountRepository.createAccount(any(Person.class))).thenThrow(new EmailAlreadyOnFileException("roy@clarkson.com"));
    SignedUpGateway gateway = mock(SignedUpGateway.class);   
    SignupController signupController = new SignupController(accountRepository, gateway);
   
    String signupJson = "{\"first-name\":\"Roy\",\"last-name\":\"Clarkson\",\"email\":\"roy@clarkson.com\",\"confirm-email\":\"roy@clarkson.com\",\"gender\":\"M\",\"birthdate\":{\"month\":7,\"day\":8,\"year\":1976},\"password\":\"letmein\"}";
    MockMvc mockMvc = standaloneSetup(signupController).build();
    mockMvc.perform(post("/signup").contentType(APPLICATION_JSON).body(signupJson.getBytes()))
        .andExpect(MockMvcResultMatchers.status().isBadRequest())
        .andExpect(MockMvcResultMatchers.jsonPath("message", Matchers.equalTo("Account creation error")))
        .andExpect(MockMvcResultMatchers.jsonPath("errors[0].field", Matchers.equalTo("email")))
        .andExpect(MockMvcResultMatchers.jsonPath("errors[0].code", Matchers.equalTo("account.duplicateEmail")))
        .andExpect(MockMvcResultMatchers.jsonPath("errors[0].message", Matchers.equalTo("already on file")));
View Full Code Here

Examples of org.springframework.test.web.server.MockMvc

    when(accountRepository.createAccount(any(Person.class))).thenThrow(new EmailAlreadyOnFileException("roy@clarkson.com"));
    SignedUpGateway gateway = mock(SignedUpGateway.class);   
    SignupController signupController = new SignupController(accountRepository, gateway);
   
    String signupJson = "{\"first-name\":null,\"last-name\":\"Clarkson\",\"email\":\"roy@clarkson.com\",\"confirm-email\":\"roy@clarkson.com\",\"gender\":\"M\",\"birthdate\":{\"month\":7,\"day\":8,\"year\":1976},\"password\":\"letmein\"}";
    MockMvc mockMvc = standaloneSetup(signupController).build();
    mockMvc.perform(post("/signup").contentType(APPLICATION_JSON).body(signupJson.getBytes()))
        .andExpect(MockMvcResultMatchers.status().isBadRequest())
        .andExpect(MockMvcResultMatchers.jsonPath("message", Matchers.equalTo("Validation error")))
        .andExpect(MockMvcResultMatchers.jsonPath("errors[0].field", Matchers.equalTo("firstName")))
        .andExpect(MockMvcResultMatchers.jsonPath("errors[0].code", Matchers.equalTo("NotEmpty")))
        .andExpect(MockMvcResultMatchers.jsonPath("errors[0].message", Matchers.equalTo("may not be empty")));
View Full Code Here

Examples of org.springframework.test.web.server.MockMvc

    when(accountRepository.createAccount(any(Person.class))).thenThrow(new EmailAlreadyOnFileException("roy@clarkson.com"));
    SignedUpGateway gateway = mock(SignedUpGateway.class);   
    SignupController signupController = new SignupController(accountRepository, gateway);
   
    String signupJson = "{\"first-name\":\"Roy\",\"last-name\":\"Clarkson\",\"email\":\"roy@clarkson.com\",\"confirm-email\":\"rclarkson@vmware.com\",\"gender\":\"M\",\"birthdate\":{\"month\":7,\"day\":8,\"year\":1976},\"password\":\"letmein\"}";
    MockMvc mockMvc = standaloneSetup(signupController).build();
    mockMvc.perform(post("/signup").contentType(APPLICATION_JSON).body(signupJson.getBytes()))
        .andExpect(MockMvcResultMatchers.status().isBadRequest())
        .andExpect(MockMvcResultMatchers.jsonPath("message", Matchers.equalTo("Validation error")))
        .andExpect(MockMvcResultMatchers.jsonPath("errors[0].field", Matchers.equalTo("email")))
        .andExpect(MockMvcResultMatchers.jsonPath("errors[0].code", Matchers.equalTo("Confirm")))
        .andExpect(MockMvcResultMatchers.jsonPath("errors[0].message", Matchers.equalTo("does not match confirmation email")));
View Full Code Here

Examples of org.springframework.test.web.servlet.MockMvc

    tokenStore.storeAccessToken(token, authentication);
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(ResourceServerContext.class);
    context.refresh();
    MockMvc mvc = MockMvcBuilders.webAppContextSetup(context)
        .addFilters(new DelegatingFilterProxy(context.getBean("springSecurityFilterChain", Filter.class)))
        .build();
    mvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.status().isUnauthorized());
    context.close();
  }
View Full Code Here

Examples of org.springframework.test.web.servlet.MockMvc

    tokenStore.storeAccessToken(token, authentication);
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(TokenServicesContext.class);
    context.refresh();
    MockMvc mvc = MockMvcBuilders.webAppContextSetup(context)
        .addFilters(new DelegatingFilterProxy(context.getBean("springSecurityFilterChain", Filter.class)))
        .build();
    mvc.perform(MockMvcRequestBuilders.get("/")).andExpect(MockMvcResultMatchers.status().isUnauthorized());
    context.close();
  }
View Full Code Here

Examples of org.springframework.test.web.servlet.MockMvc

    tokenStore.storeAccessToken(token, authentication);
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(TokenExtractorContext.class);
    context.refresh();
    MockMvc mvc = MockMvcBuilders.webAppContextSetup(context)
        .addFilters(new DelegatingFilterProxy(context.getBean("springSecurityFilterChain", Filter.class)))
        .build();
    mvc.perform(MockMvcRequestBuilders.get("/").header("Authorization", "Bearer BAR")).andExpect(
        MockMvcResultMatchers.status().isNotFound());
    context.close();
  }
View Full Code Here

Examples of org.springframework.test.web.servlet.MockMvc

  public void testAuthCodeRedirect() throws Exception {
    AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
    context.setServletContext(new MockServletContext());
    context.register(ClientContext.class);
    context.refresh();
    MockMvc mvc = MockMvcBuilders.webAppContextSetup(context).addFilters(new OAuth2ClientContextFilter()).build();
    mvc.perform(MockMvcRequestBuilders.get("/photos"))
        .andExpect(MockMvcResultMatchers.status().isFound())
        .andExpect(
            MockMvcResultMatchers.header().string("Location",
                CoreMatchers.startsWith("http://example.com/authorize")));
    context.close();
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.