How do I set up unit tests for RepositoryRestController in Spring?

守給你的承諾、 提交于 2020-01-25 06:43:26

问题


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
        }
    }

}

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?

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.


回答1:


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 {

}


来源:https://stackoverflow.com/questions/48092244/how-do-i-set-up-unit-tests-for-repositoryrestcontroller-in-spring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!