【发布时间】:2017-10-10 03:50:01
【问题描述】:
我有一个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?还是我可以在这里使用的其他方法?
【问题讨论】:
-
如果将
CustomerController类公开会发生什么? -
抱歉,这是一个 C&P 错误,与这个问题无关(不过很好!)。这里的问题是 spring 认为它需要传递一个 PERA,但没有一个。几乎可以肯定,问题出在我的测试配置上。
标签: java spring spring-data-jpa spring-test spring-rest