如何在 Spring 中为 RepositoryRestController 设置单元测试? [英] How do I set up unit tests for RepositoryRestController in Spring?

查看:42
本文介绍了如何在 Spring 中为 RepositoryRestController 设置单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于 RepositoryRestController(MyController 类)的单元测试,我有以下配置:

I have the following configuration for my unit test for a RepositoryRestController (the MyController class):

@RunWith(SpringRunner.class)
@WebMvcTest
public class MyTest {

    @Configuration
    @EnableSpringDataWebSupport
    static class TestConfiguration {

        @Bean
        MyController myController() {
            //controller construction
        }
    }

}

我找不到任何在线文档来说明我需要设置什么样的配置才能使测试正确运行.当我在我正在测试的控制器方法上注入 Pageable 实例时遇到问题,我只是偶然发现了 EnableSpringDataWebSupport.现在,我的问题是 PersistentEntityResourceAssembler 没有自动装配,因为日志说它没有默认构造函数.Spring 是如何设置的?

I can't find any documents online expounding on what sort of config I need to setup to get the tests running correctly. I only stumbled upon EnableSpringDataWebSupport when I was having trouble injecting Pageable instance on the controller method I'm testing. Now, my problem is that PersistentEntityResourceAssembler doesn't get autowired because the log says it has no default constructor. How does Spring setup this up?

至于与这个问题的重复,请注意我的配置与接受的答案并没有太大不同,我'我还是有问题.@WebMvcTest 意味着 @AutoConfigureMockMvc.在发布这个问题之前,我已经用尽了我能搜索到的关于这个问题的大部分信息.

As for the duplication with this question, notice that my configuration is not all that much different from the accepted answer and I'm still having problems. @WebMvcTest implies @AutoConfigureMockMvc. I've exhausted most of the information I could search about this problem before posting this question.

推荐答案

如果你的控制器中有@PageableDefault,这是控制器测试的一般设置.

If you have @PageableDefault in your controller, this is the general setting for controller test.

@RunWith(SpringRunner.class)
@WebMvcTest(YourController.class)
@EnableSpringDataWebSupport  // for Pageable resolve
public class YourControllerTest {

}

这篇关于如何在 Spring 中为 RepositoryRestController 设置单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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