2

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.

Psycho Punch
  • 6,418
  • 9
  • 53
  • 86
  • check this out https://stackoverflow.com/questions/42962330/rest-controllers-vs-spring-data-rest-repositoryrestresource – pvpkiran Jan 04 '18 at 10:03
  • Possible duplicate of [Testing a custom RepositoryRestController that uses a PersistentEntityResourceAssembler](https://stackoverflow.com/questions/46658333/testing-a-custom-repositoryrestcontroller-that-uses-a-persistententityresourceas) – tkruse Jan 05 '18 at 01:21
  • Also see here: http://www.springboottutorial.com/unit-testing-for-spring-boot-rest-services – tkruse Jan 05 '18 at 01:21
  • Downvoting because question is very broad (many ways to test with Spring), and spring has excellent documentation that should help getting much further than what the question provides. – tkruse Jan 05 '18 at 01:22
  • 1
    @tkruse Then you should have pointed me to where the documentation is. I know how to test a regular REST controller, and I have specified in the configuration I'm currently using and the specific issue I'm having trouble with. I have also seen that link to that SO question before I posted this question. You should know that the answer to that question is pretty much what I have on my setup already. – Psycho Punch Jan 05 '18 at 01:51
  • also possible duplicate of https://stackoverflow.com/questions/39311001/junit-for-restcontroller-with-parameter-persistententityresourceassembler – tkruse Jan 05 '18 at 01:59
  • Also you should add the log error output – tkruse Jan 05 '18 at 02:19

1 Answers1

0

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 {

}
Min Hyoung Hong
  • 1,102
  • 9
  • 13