spring boot test unable to inject TestRestTemplate and MockMvc

后端 未结 4 1780
我在风中等你
我在风中等你 2021-01-07 16:10

I am using spring boot 1.4.0.RELEASE. I am writing tests for my controller class. I get the following exception.

org.springframework.beans.facto         


        
4条回答
  •  粉色の甜心
    2021-01-07 16:51

    To work with that, don't use a deprecated TestRestTemplate.

    Deprecated:

    import org.springframework.boot.test.TestRestTemplate;
    

    Correct:

    import org.springframework.boot.test.web.client.TestRestTemplate;
    

    Then you can use the @Autowired annotation in your class:

    @Autowired
    private TestRestTemplate restTemplate;
    

    And don't use:

    @Autowired
    private MockMvc mvc;
    

    Both together doesn't work.

提交回复
热议问题