测试使用 PersistentEntityResourceAssembler 的自定义 RepositoryRestController

2023-12-02

我有一个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?或者我可以在这里使用另一种方法?


我现在结束了:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc

它的缺点是测试将启动完整的 Spring 应用程序上下文(但没有服务器)。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

测试使用 PersistentEntityResourceAssembler 的自定义 RepositoryRestController 的相关文章

随机推荐