Testing a custom RepositoryRestController that uses a PersistentEntityResourceAssembler

后端 未结 2 1659
深忆病人
深忆病人 2021-01-14 11:36

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

I have a method on my controller that takes a PersistentEnt

相关标签:
2条回答
  • 2021-01-14 11:57

    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.

    0 讨论(0)
  • 2021-01-14 12:13

    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).

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