spring-test-mvc

Avoid Controllers initialization when testing spring boot HandlerInterceptor

僤鯓⒐⒋嵵緔 提交于 2019-12-12 04:11:50
问题 I'm trying to find the right configuration for testing HandlerInterceptor of a Spring-boot application, with @MockBean dependencies, but without initializing the whole Bean pool, because some Controllers have @PostConstruct calls that can't be mocked (knowing that @Before call comes after @PostContruct Controller call). For now I have come to this syntax : @RunWith(SpringJUnit4ClassRunner.class) @SpringBootTest(classes = Application.class) public class MyHandlerInterceptorTest { @Autowired

How to use MockMvc with mock of controller

痴心易碎 提交于 2019-12-11 23:33:00
问题 I have this contoller method: @RequestMapping(value = "/addEvent", method = RequestMethod.POST) public String addEvent(Model model, @Valid @ModelAttribute("myEvent") Event event, BindingResult result, RedirectAttributes redirectAttributes, @RequestParam(required = true) Integer selectedEventTypeId, @RequestParam(required = true) Integer selectedEventStatusId) { if (result.getErrorCount() > 1 ){ return "eventDetailsAdd"; } eventService.addEvent(event, selectedEventTypeId, selectedEventStatusId

How to mock BindingResult in Spring Boot Test

不想你离开。 提交于 2019-12-11 07:58:12
问题 I have the following controller: @RestController @RequestMapping(value = ROOT_MAPPING) public class GatewayController { @Autowired private RequestValidator requestValidator; @InitBinder protected void initBinder(WebDataBinder binder) { binder.addValidators(requestValidator); } @PostMapping(value = REDIRECT_MAPPING) public ResponseEntity<ResponseDTO> redirectEndpoint(@Validated @RequestBody RequestDTO requestDTO, BindingResult result) { if (result.hasErrors()) { // Handle validation errors

Spring MVC unit test for DeferredResult doesn't call timeout callback

て烟熏妆下的殇ゞ 提交于 2019-12-11 04:25:46
问题 I'm using Spring 4.3.18 and Spring Boot 1.5.14 on Java 7. I'm implementing a RestController endpoint which returns a DeferredResult with a timeout callback. I'm trying to write a unit test for the timeout callback, but I can't get a MockMvc unit test to call the timeout callback. For the sake of testing, I wrote this endpoint: @PostMapping("/test") public DeferredResult<String> testit() { logger.info("testit called"); final DeferredResult<String> rv = new DeferredResult<>(1000L); rv.onTimeout

Why spring test is fail, does not work @MockBean

て烟熏妆下的殇ゞ 提交于 2019-12-10 20:56:42
问题 I try to create my first the test for a simple spring-boot controller but I get Handler: Type = null . In browser code is work but a test fails. My App use spring-security. Please help me fix it an issue and understand my mistake. Thank You. This is controller: private final ItemService service; @GetMapping("/get_all_items") public String getAllItems(Model model) { model.addAttribute("items", service.getAll()); return "all_items"; } This is a test. @RunWith(SpringRunner.class) @WebMvcTest

@ModelAttribute controller spring-mvc mocking

家住魔仙堡 提交于 2019-12-10 17:29:56
问题 I want to test a controller which is using @ModelAttribute for one of its method arguments. public String processSaveAction(@ModelAttribute("exampleEntity") ExampleEntity exampleEntity) @ModelAttribute method getExampleEntity is using @RequestParam : @ModelAttribute("exampleEntity") public ExampleEntity getExampleEntity(@RequestParam(value = "id", required = true) ExampleEntity exampleEntity) { My controller is using WebDataBinder to call a factory, which returns an object based on param "id"

Spring MVC Test Framework - Unsupported Media Type

岁酱吖の 提交于 2019-12-10 17:19:17
问题 I am writing a test for controller using spring test mvc framework. I am trying to test a post request and it produces JSON. The actual code is running but test is failing. I an getting the error Status expected 200 but getting 415 which means unsupported media type. i check all examples on net and it seems my implementation is correct. please help me out. MyTestcase TestStringPost() is running successfully but TestJSONPost is failing. I am using Spring 3.2 My Controller @Controller public

Testing Spring Security and MvcMock using a custom UserDetails implementation

痞子三分冷 提交于 2019-12-10 14:36:55
问题 I'm trying to follow this article [1] to mock security in my Spring MvcMock test. The REST service I want to test looks like this: @RequestMapping(value = "/something/{id}", method = RequestMethod.DELETE) public ResponseEntity<Void> deleteXXX(@ActiveUser AppUser user, @PathVariable(value = "id") Long id) { ... } where @ActiveUser is a custom implementation/extension of @AuthenticationPrincipal and AppUser is the custom UserDetails implementation. In the test I do this: mockMvc.perform(delete(

How to mock Eureka when doing Integration Tests in Spring?

心不动则不痛 提交于 2019-12-09 16:26:36
问题 I am running a simple Junit Testing a Controller in Spring Boot. The test code looks like this: @RunWith(SpringJUnit4ClassRunner.class) @SpringApplicationConfiguration(classes = {FrontControllerApplication.class}) @WebAppConfiguration @ComponentScan @IntegrationTest({"server.port:0", "eureka.client.registerWithEureka:false", "eureka.client.fetchRegistry:false"}) @ActiveProfiles("integrationTest") public class MyControllerIT { In the application-integrationTest.properties I have the following

How to PUT multipart/form-data using Spring MockMvc?

自作多情 提交于 2019-12-08 15:58:41
问题 I have controller's method with PUT method, which receives multipart/form-data: @RequestMapping(value = "/putIn", method = RequestMethod.PUT) public Foo updateFoo(HttpServletRequest request, @RequestBody Foo foo, @RequestParam("foo_icon") MultipartFile file) { ... } and I want to test it using MockMvc . Unfortunately MockMvcRequestBuilders.fileUpload creates essentially instance of MockMultipartHttpServletRequestBuilder which has POST method: super(HttpMethod.POST, urlTemplate, urlVariables)