public

Sample Web Config test - with pathvariables

  • Download Gist
WebContextConfigurationTest.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
package org.bk.webtest2;
 
import static org.springframework.test.web.server.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.server.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.server.setup.MockMvcBuilders.*;
 
import org.junit.Test;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.test.web.server.MockMvc;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
 
public class WebContextConfigurationTest {
 
@Test
public void testAWebFlow() throws Exception {
MockMvc mockMvc = annotationConfigSetup(WebContextConfigurationTest.TestConfiguration.class).build();
mockMvc.perform(get("/sample")).andExpect(view().name("sampleProperView"));
mockMvc.perform(get("/section")).andExpect(view().name("sectionView"));
}
 
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "org.bk.webtest2")
public static class TestConfiguration {
//
}
}
 
@Controller
class TestController {
@RequestMapping(value = "/sample")
public String sample() {
return "sampleProperView";
}
 
@RequestMapping(value = "/{section}")
public String section(@PathVariable("section") String section) {
return section + "View";
 
}
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.