测试使用PersistentEntityResourceAssembler的自定义RepositoryRestController [英] Testing a custom RepositoryRestController that uses a PersistentEntityResourceAssembler

查看:207
本文介绍了测试使用PersistentEntityResourceAssembler的自定义RepositoryRestController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RepositoryRestController,它公开了一些持久实体的资源.

I have a RepositoryRestController that exposes resources for some persistent entities.

我的控制器上有一个方法,该方法采用PersistentEntityResourceAssembler来帮助我自动生成资源.

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)

我想为我的控制器编写一个测试(我的实际用例实际上值得测试),并计划使用@WebMvcTest.

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.

所以我有以下测试课:

@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>()

But I get an exception saying java.lang.NoSuchMethodException: org.springframework.data.rest.webmvc.PersistentEntityResourceAssembler.<init>()

大概是由于我丢失了整个数据层,因此此处的配置不正确.有什么方法可以模拟PersistentEntityResourceAssembler吗?还是我可以在这里使用的另一种方法?

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?

推荐答案

我现在的结局是:

@RunWith(SpringRunner.class)
@SpringBootTest
@AutoConfigureMockMvc

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

The downsite of it is that the test would start the full Spring application context (but without the server).

这篇关于测试使用PersistentEntityResourceAssembler的自定义RepositoryRestController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆