Testing a custom RepositoryRestController that uses a PersistentEntityResourceAssembler

◇◆丶佛笑我妖孽 提交于 2019-12-01 06:53:22

问题


I have a RepositoryRestController that exposes resources for some persistent entities.

I have a method on my controller that takes a PersistentEntityResourceAssembler to help me generate the resources automatically.

@RepositoryRestController
@ExposesResourceFor(Customer.class)
@RequestMapping("/api/customers")
public class CustomerController {

    @Autowired
    private CustomerService service;

    @RequestMapping(method = GET, value="current")
    public ResponseEntity getCurrent(Principal principal Long id, PersistentEntityResourceAssembler assembler) {
        return ResponseEntity.ok(assembler.toResource(service.getForPrincipal(principal)));
    }
}

(Contrived example, but it saves going into too much detail about irrelevant details of my use-case)

I'd like to write a test for my controller (my real use-case is actually worth testing), and am planning on making use of @WebMvcTest.

So I have the following test class:

@RunWith(SpringRunner.class)
@WebMvcTest(CustomerController.class)
@AutoConfigureMockMvc(secure=false)
public class CustomerControllerTest {
    @Autowired
    private MockMvc client;

    @MockBean
    private CustomerService service;

    @Test
    public void testSomething() {
        // test stuff in here
    }

    @Configuration
    @Import(CustomerController.class)
    static class Config {
    }

}

But I get an exception saying java.lang.NoSuchMethodException: org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.<init>()

Presumably something is not being configured correctly here because I'm missing the entire data layer. Is there some way of mocking out the PersistentEntityResourceAssembler? Or another approach I could use here?


回答1:


I ended up for now with:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc

The downsite of it is that the test would start the full Spring application context (but without the server).




回答2:


I ended up doing a slightly hacky solution here:

  • I removed PersistentEntityResourceAssembler from the controller method.
  • I added an @Autowired RepositoryEntityLinks to the controller, on which I call linkToSingleResource to create the links as needed.
  • I added an @MockBean RepositoryEntityLinks to my test class, and configured the mocking to return something sensible:

    given(repositoryEntityLinks.linkToSingleResource(any(Identifiable.class)))
            .willAnswer(invocation -> {
                final Identifiable identifiable = (Identifiable) invocation.getArguments()[0];
                return new Link("/data/entity/" + identifiable.getId().toString());
            });
    

It's far from ideal - I'd love to know if there's a way of getting just enough of the data layer up that I can depend on PersistentEntityResourceAssembler.



来源:https://stackoverflow.com/questions/46658333/testing-a-custom-repositoryrestcontroller-that-uses-a-persistententityresourceas

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!