JUnit for RestController with parameter PersistentEntityResourceAssembler

前端 未结 2 908
执念已碎
执念已碎 2021-01-25 00:52

could you please help me.

I try to implement an JUnit-Test for a Restcontroller with parameter PersistentEntityResourceAssembler.

Here is my RestController:

相关标签:
2条回答
  • 2021-01-25 00:58

    It seems that you have missed something. I faced this error and fixed it, the problem was that I forgot to add an annotation @RequestBody to the parameter of the method. Next code works fine:

    //interface:
    @ResponseStatus(value = HttpStatus.ACCEPTED)
    @RequestMapping(method = RequestMethod.POST, produces = { MediaType.APPLICATION_JSON_VALUE }, consumes = { MediaType.APPLICATION_JSON_VALUE })
    public Message greeting(@RequestBody GreetingRequestMessage requestMessage);
    
    //implementation:
    public Message greeting(@RequestBody GreetingRequestMessage requestMessage) {
    }
    
    //setup @Before:
    mvc = MockMvcBuilders.standaloneSetup(testObj).build();
    GreetingRequestMessage requestMessage = new GreetingRequestMessage();
    payloadString = mapper.writeValueAsString(requestMessage);
    
    //test:
    payloadString = mapper.writeValueAsString(request);
    mvc.perform(
                MockMvcRequestBuilders.post("/greeting")
                        .content(payloadString)
                        .contentType(MediaType.APPLICATION_JSON).accept(MediaType.APPLICATION_JSON))
                .andExpect(status().is(202));
    
    0 讨论(0)
  • 2021-01-25 01:12

    From your controller method I don't see if you are using PersistentEntityResourceAssembler. Please check your use case. To fix this, in your controller, you can use @RepositoryRestController instead of @RestController. Please check http://docs.spring.io/spring-data/rest/docs/current/reference/html/#customizing-sdr.overriding-sdr-response-handlers. I found the similar issue has been reported for some different use case. This issue is still in open state. https://jira.spring.io/browse/DATAREST-657

    0 讨论(0)
提交回复
热议问题