Testing a custom RepositoryRestController that uses a PersistentEntityResourceAssembler

后端 未结 2 1679
深忆病人
深忆病人 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.

提交回复
热议问题