测试使用 PersistentEntityResourceAssembler 的自定义 RepositoryRestController
Testing a custom RepositoryRestController that uses a PersistentEntityResourceAssembler
我有一个 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。
所以我有以下测试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 {
}
}
但是我得到一个例外 java.lang.NoSuchMethodException: org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.<init>()
可能是这里配置不正确,因为我缺少整个数据层。有什么方法可以模拟 PersistentEntityResourceAssembler
吗?或者我可以在这里使用的另一种方法?
我最后在这里做了一个有点老套的解决方案:
- 我从控制器方法中删除了
PersistentEntityResourceAssembler
。
- 我向控制器添加了一个
@Autowired RepositoryEntityLinks
,我在控制器上调用 linkToSingleResource
以根据需要创建链接。
我在测试 class 中添加了一个 @MockBean RepositoryEntityLinks
,并将模拟配置为 return 一些合理的东西:
given(repositoryEntityLinks.linkToSingleResource(any(Identifiable.class)))
.willAnswer(invocation -> {
final Identifiable identifiable = (Identifiable) invocation.getArguments()[0];
return new Link("/data/entity/" + identifiable.getId().toString());
});
这远非理想 - 我很想知道是否有一种方法可以获得我可以依赖的足够的数据层 PersistentEntityResourceAssembler
。
我现在结束了:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
它的缺点是测试将启动完整的 Spring 应用程序上下文(但没有服务器)。
我有一个 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。
所以我有以下测试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 {
}
}
但是我得到一个例外 java.lang.NoSuchMethodException: org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.<init>()
可能是这里配置不正确,因为我缺少整个数据层。有什么方法可以模拟 PersistentEntityResourceAssembler
吗?或者我可以在这里使用的另一种方法?
我最后在这里做了一个有点老套的解决方案:
- 我从控制器方法中删除了
PersistentEntityResourceAssembler
。 - 我向控制器添加了一个
@Autowired RepositoryEntityLinks
,我在控制器上调用linkToSingleResource
以根据需要创建链接。 我在测试 class 中添加了一个
@MockBean RepositoryEntityLinks
,并将模拟配置为 return 一些合理的东西:given(repositoryEntityLinks.linkToSingleResource(any(Identifiable.class))) .willAnswer(invocation -> { final Identifiable identifiable = (Identifiable) invocation.getArguments()[0]; return new Link("/data/entity/" + identifiable.getId().toString()); });
这远非理想 - 我很想知道是否有一种方法可以获得我可以依赖的足够的数据层 PersistentEntityResourceAssembler
。
我现在结束了:
@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc
它的缺点是测试将启动完整的 Spring 应用程序上下文(但没有服务器)。