3

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?

Martin
  • 3,703
  • 2
  • 21
  • 43
  • What happens if you make the `CustomerController` class public? – Sam Brannen Oct 11 '17 at 12:54
  • Sorry, that was a C&P mistake, and not related to this problem (nice catch though!). The problem here is that spring sees it needs to pass a PERA through, but doesn't have one. The problem is almost certainly with my test configuration. – Martin Oct 11 '17 at 21:36

2 Answers2

3

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

KLHauser
  • 856
  • 5
  • 11
0

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.

Martin
  • 3,703
  • 2
  • 21
  • 43