我有一个RepositoryRestController
公开一些持久性实体的资源。
我的控制器上有一个方法,需要PersistentEntityResourceAssembler
帮助我自动生成资源。
@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)));
}
}
(人为的示例,但它节省了关于我的用例的不相关细节的太多细节)
我想为我的控制器编写一个测试(我的真实用例实际上值得测试),并计划使用@WebMvcTest。
所以我有以下测试类:
@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 {
}
}
但我得到一个例外说java.lang.NoSuchMethodException: org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.<init>()
大概这里有些东西配置不正确,因为我丢失了整个数据层。有什么方法可以嘲笑PersistentEntityResourceAssembler
?或者我可以在这里使用另一种方法?