Java Code Examples for org.springframework.test.web.servlet.MvcResult#getModelAndView()

The following examples show how to use org.springframework.test.web.servlet.MvcResult#getModelAndView() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestSupportWithMVC.java    From rebuild with GNU General Public License v3.0 6 votes vote down vote up
/**
 * @param builder
 * @param user
 * @param expect
 * @return
 * @throws Exception
 */
protected MvcResponse perform(MockHttpServletRequestBuilder builder, ID user, ResultMatcher expect) throws Exception {
    builder.contentType("text/plain; charset=utf-8")
            .accept(MediaType.APPLICATION_JSON_UTF8)
            .header(HttpHeaders.USER_AGENT, UA_WIN10_CHROME);
    if (user != null) {
        builder.sessionAttr(WebUtils.CURRENT_USER, user);
    }

    ResultActions resultActions = springMVC.perform(builder);
    if (expect == null) {
        resultActions.andExpect(MockMvcResultMatchers.status().isOk());
    } else {
        resultActions.andExpect(expect);
    }

    MvcResult result = resultActions.andReturn();

    String content = result.getResponse().getContentAsString();
    if (StringUtils.isNotBlank(content)) {
        return new MvcResponse(content);
    }

    ModelAndView view = result.getModelAndView();
    return new MvcResponse(view);
}
 
Example 2
Source File: ModelResultMatchers.java    From spring-analysis-note with MIT License 5 votes vote down vote up
private ModelAndView getModelAndView(MvcResult mvcResult) {
	ModelAndView mav = mvcResult.getModelAndView();
	if (mav == null) {
		fail("No ModelAndView found");
	}
	return mav;
}
 
Example 3
Source File: WelcomeControllerTest.java    From spring-boot with MIT License 5 votes vote down vote up
@Test
public void main() throws Exception {
    ResultActions resultActions = mockMvc.perform(get("/"))
            .andExpect(status().isOk())
            .andExpect(view().name("welcome"))
            .andExpect(model().attribute("message", equalTo("Mkyong")))
            .andExpect(model().attribute("tasks", is(expectedList)))
            .andExpect(content().string(containsString("Hello, Mkyong")));

    MvcResult mvcResult = resultActions.andReturn();
    ModelAndView mv = mvcResult.getModelAndView();
    //
}
 
Example 4
Source File: ModelResultMatchers.java    From java-technology-stack with MIT License 5 votes vote down vote up
private ModelAndView getModelAndView(MvcResult mvcResult) {
	ModelAndView mav = mvcResult.getModelAndView();
	if (mav == null) {
		fail("No ModelAndView found");
	}
	return mav;
}
 
Example 5
Source File: JspViewTest.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Test
void testViewRender() throws Exception {
    ResultActions resultActions = mockMvc.perform(get("/jsp"));
    MvcResult mvcResult = resultActions.andReturn();

    verifySpanCapture("InternalResource", " message-view", mvcResult.getResponse(), "");

    ModelAndView modelAndView = mvcResult.getModelAndView();
    assertEquals(modelAndView.getModel().get("message"), "Message 123");
}
 
Example 6
Source File: ViewResultMatchers.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Assert the selected view name with the given Hamcrest {@link Matcher}.
 */
public ResultMatcher name(final Matcher<? super String> matcher) {
	return new ResultMatcher() {
		@Override
		public void match(MvcResult result) throws Exception {
			ModelAndView mav = result.getModelAndView();
			assertTrue("No ModelAndView found", mav != null);
			assertThat("View name", mav.getViewName(), matcher);
		}
	};
}
 
Example 7
Source File: ViewResultMatchers.java    From spring4-understanding with Apache License 2.0 5 votes vote down vote up
/**
 * Assert the selected view name.
 */
public ResultMatcher name(final String expectedViewName) {
	return new ResultMatcher() {
		@Override
		public void match(MvcResult result) throws Exception {
			ModelAndView mav = result.getModelAndView();
			assertTrue("No ModelAndView found", mav != null);
			assertEquals("View name", expectedViewName, mav.getViewName());
		}
	};
}
 
Example 8
Source File: ClusterConfigControllerTest.java    From kafka-webview with MIT License 4 votes vote down vote up
/**
 * Test creating new ssl cluster but not providing a keystore will fail validation.
 */
@Test
@Transactional
public void testPostUpdate_newSslCluster_missingKeyStore_failsValidation() throws Exception {
    final String expectedClusterName = "My New Cluster Name" + System.currentTimeMillis();
    final String expectedBrokerHosts = "localhost:9092";
    final String expectedKeystorePassword = "KeyStorePassword";
    final String expectedTruststorePassword = "TrustStorePassword";

    final MockMultipartFile trustStoreUpload = new MockMultipartFile("trustStoreFile", "TrustStoreFile".getBytes(Charsets.UTF_8));

    // Hit create page.
    final MvcResult result = mockMvc
        .perform(multipart("/configuration/cluster/update")
            .file(trustStoreUpload)
            .with(user(adminUserDetails))
            .with(csrf())
            .param("name", expectedClusterName)
            .param("brokerHosts", expectedBrokerHosts)
            .param("ssl", "true")
            .param("trustStorePassword", expectedTruststorePassword)
            .param("keyStorePassword", expectedKeystorePassword)
        )
        //.andDo(print())
        .andExpect(status().is2xxSuccessful())
        .andExpect(content().string(containsString("Select a KeyStore JKS to upload")))
        .andReturn();

    // Verify Response
    final ModelAndView modelAndView = result.getModelAndView();
    assertEquals(
        "Should have reloaded existing view",
        "configuration/cluster/create",
        modelAndView.getViewName()
    );
    assertTrue("Should have clusterForm attribute", modelAndView.getModel().containsKey("clusterForm"));

    assertTrue("Should have bound errors", modelAndView.getModel().containsKey("org.springframework.validation.BindingResult.clusterForm"));
    final Errors validationResult = (Errors) modelAndView.getModel().get("org.springframework.validation.BindingResult.clusterForm");
    assertEquals("Should have 1 error", 1, validationResult.getErrorCount());
    assertNotNull("Should have field error on keyStoreFile field", validationResult.getFieldError("keyStoreFile"));

    // Lookup Cluster
    final Cluster cluster = clusterRepository.findByName(expectedClusterName);
    assertNull("Should have no new cluster", cluster);
}
 
Example 9
Source File: ModelResultMatchers.java    From spring4-understanding with Apache License 2.0 4 votes vote down vote up
private ModelAndView getModelAndView(MvcResult mvcResult) {
	ModelAndView mav = mvcResult.getModelAndView();
	assertTrue("No ModelAndView found", mav != null);
	return mav;
}