spring-mvc-test

Mock MVC - Add Request Parameter to test

南笙酒味 提交于 2019-11-30 10:42:48
问题 I am using spring 3.2 mock mvc to test my controller.My code is @Autowired private Client client; @RequestMapping(value = "/user", method = RequestMethod.GET) public String initUserSearchForm(ModelMap modelMap) { User user = new User(); modelMap.addAttribute("User", user); return "user"; } @RequestMapping(value = "/byName", method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public @ResponseBody String getUserByName(@RequestParam("firstName") String firstName, @RequestParam("lastName")

Mock MVC - Add Request Parameter to test

萝らか妹 提交于 2019-11-29 22:00:59
I am using spring 3.2 mock mvc to test my controller.My code is @Autowired private Client client; @RequestMapping(value = "/user", method = RequestMethod.GET) public String initUserSearchForm(ModelMap modelMap) { User user = new User(); modelMap.addAttribute("User", user); return "user"; } @RequestMapping(value = "/byName", method = RequestMethod.GET) @ResponseStatus(HttpStatus.OK) public @ResponseBody String getUserByName(@RequestParam("firstName") String firstName, @RequestParam("lastName") String lastName, @ModelAttribute("userClientObject") UserClient userClient) { return client

How to test DeferredResult timeoutResult

我只是一个虾纸丫 提交于 2019-11-29 05:56:17
I'm implementing long polling as per the Spring blog from some time ago . Here my converted method with same response signature as before, but instead of responding immediately, it now uses long polling: private Map<String, DeferredResult<ResponseEntity<?>>> requests = new ConcurrentHashMap<>(); @RequestMapping(value = "/{uuid}", method = RequestMethod.GET) public DeferredResult<ResponseEntity<?>> poll(@PathVariable("uuid") final String uuid) { // Create & store a new instance ResponseEntity<?> pendingOnTimeout = ResponseEntity.accepted().build(); DeferredResult<ResponseEntity<?>>

Testing Spring MVC @ExceptionHandler method with Spring MVC Test

二次信任 提交于 2019-11-28 19:31:01
问题 I have the following simple controller to catch any unexpected exceptions: @ControllerAdvice public class ExceptionController { @ExceptionHandler(Throwable.class) @ResponseStatus(value = HttpStatus.INTERNAL_SERVER_ERROR) @ResponseBody public ResponseEntity handleException(Throwable ex) { return ResponseEntityFactory.internalServerErrorResponse("Unexpected error has occurred.", ex); } } I'm trying to write an integration test using Spring MVC Test framework. This is what I have so far:

Isolated Controller Test can't instantiate Pageable

被刻印的时光 ゝ 提交于 2019-11-28 09:36:20
I have a Spring MVC Controller which uses Pagination Support of Spring-Data: @Controller public class ModelController { private static final int DEFAULT_PAGE_SIZE = 50; @RequestMapping(value = "/models", method = RequestMethod.GET) public Page<Model> showModels(@PageableDefault(size = DEFAULT_PAGE_SIZE) Pageable pageable, @RequestParam( required = false) String modelKey) { //.. return models; } } And I'd like to test the RequestMapping using the nice Spring MVC Test Support. In order to keep these tests fast and isolated from all the other stuff going on, I do not want to create the complete

How to test DeferredResult timeoutResult

不想你离开。 提交于 2019-11-27 23:44:18
问题 I'm implementing long polling as per the Spring blog from some time ago. Here my converted method with same response signature as before, but instead of responding immediately, it now uses long polling: private Map<String, DeferredResult<ResponseEntity<?>>> requests = new ConcurrentHashMap<>(); @RequestMapping(value = "/{uuid}", method = RequestMethod.GET) public DeferredResult<ResponseEntity<?>> poll(@PathVariable("uuid") final String uuid) { // Create & store a new instance ResponseEntity<?

Isolated Controller Test can't instantiate Pageable

馋奶兔 提交于 2019-11-27 03:02:19
问题 I have a Spring MVC Controller which uses Pagination Support of Spring-Data: @Controller public class ModelController { private static final int DEFAULT_PAGE_SIZE = 50; @RequestMapping(value = "/models", method = RequestMethod.GET) public Page<Model> showModels(@PageableDefault(size = DEFAULT_PAGE_SIZE) Pageable pageable, @RequestParam( required = false) String modelKey) { //.. return models; } } And I'd like to test the RequestMapping using the nice Spring MVC Test Support. In order to keep

How to avoid the “Circular view path” exception with Spring MVC test

对着背影说爱祢 提交于 2019-11-26 12:53:55
I have the following code in one of my controllers: @Controller @RequestMapping("/preference") public class PreferenceController { @RequestMapping(method = RequestMethod.GET, produces = "text/html") public String preference() { return "preference"; } } I am simply trying to test it using Spring MVC test as follows: @ContextConfiguration @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) public class PreferenceControllerTest { @Autowired private WebApplicationContext ctx; private MockMvc mockMvc; @Before public void setup() { mockMvc = webAppContextSetup(ctx).build(); } @Test public

How to avoid the “Circular view path” exception with Spring MVC test

China☆狼群 提交于 2019-11-26 05:53:23
问题 I have the following code in one of my controllers: @Controller @RequestMapping(\"/preference\") public class PreferenceController { @RequestMapping(method = RequestMethod.GET, produces = \"text/html\") public String preference() { return \"preference\"; } } I am simply trying to test it using Spring MVC test as follows: @ContextConfiguration @WebAppConfiguration @RunWith(SpringJUnit4ClassRunner.class) public class PreferenceControllerTest { @Autowired private WebApplicationContext ctx;