【问题标题】:How do I set up unit tests for RepositoryRestController in Spring?如何在 Spring 中为 RepositoryRestController 设置单元测试?
【发布时间】:2018-06-14 00:07:32
【问题描述】:

我对@9​​87654325@(MyController 类)的单元测试有以下配置:

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

    @Configuration
    @EnableSpringDataWebSupport
    static class TestConfiguration {

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

}

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

至于与this question 的重复,请注意我的配置与接受的答案并没有太大不同,我仍然遇到问题。 @WebMvcTest 暗示 @AutoConfigureMockMvc。在发布此问题之前,我已经用尽了可以搜索的有关此问题的大部分信息。

【问题讨论】:

标签: spring unit-testing spring-mvc spring-boot spring-data-rest


【解决方案1】:

如果您的控制器中有@PageableDefault,这是控制器测试的常规设置。

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-30
    • 2014-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-02
    • 1970-01-01
    相关资源
    最近更新 更多