如何在Spring中为RepositoryRestController设置单元测试?

时间:2018-01-04 09:29:57

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

我对RepositoryRestControllerMyController类)的单元测试有以下配置:

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

    @Configuration
    @EnableSpringDataWebSupport
    static class TestConfiguration {

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

}

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

至于与this question的重复,请注意我的配置与接受的答案没有太大差别,我仍然遇到问题。 @WebMvcTest隐含@AutoConfigureMockMvc。在发布此问题之前,我已经用尽了大部分关于此问题的信息。

1 个答案:

答案 0 :(得分:0)

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

def __import__(self, *args, **kwrgs):
    raise ImportError("Imports are not allowed")

import math

print math.sqrt(4)