@Autowired - No qualifying bean of type found for dependency

前端 未结 17 1531
自闭症患者
自闭症患者 2020-12-07 15:00

I\'ve started my project by creating entities, services and JUnit tests for services using Spring and Hibernate. All of this works great. Then I\'ve added spring-mvc to make

相关标签:
17条回答
  • 2020-12-07 15:18

    I had this happen because I added an autowired dependency to my service class but forgot to add it to the injected mocks in my service unit test.

    The unit test exception appeared to report a problem in the service class when the problem was actually in the unit test. In retrospect, the error message told me exactly what the problem was.

    0 讨论(0)
  • 2020-12-07 15:19

    Faced the same issue in my spring boot application even though I had my package specific scans enabled like

    @SpringBootApplication(scanBasePackages={"com.*"})
    

    But, the issue was resolved by providing @ComponentScan({"com.*"}) in my Application class.

    0 讨论(0)
  • 2020-12-07 15:20
     <context:component-scan base-package="com.*" />
    

    same issue arrived , i solved it by keeping the annotations intact and in dispatcher servlet :: keeping the base package scan as com.*. this worked for me.

    0 讨论(0)
  • 2020-12-07 15:24

    Instead of @Autowire MailManager mailManager, you can mock the bean as given below:

    import org.springframework.boot.test.mock.mockito.MockBean;
    
    ::
    ::
    
    @MockBean MailManager mailManager;
    

    Also, you can configure @MockBean MailManager mailManager; separately in the @SpringBootConfiguration class and initialize like below:

    @Autowire MailManager mailManager
    
    0 讨论(0)
  • 2020-12-07 15:25

    The thing is that both the application context and the web application context are registered in the WebApplicationContext during server startup. When you run the test you must explicitly tell which contexts to load.

    Try this:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:/beans.xml", "/mvc-dispatcher-servlet.xml"})
    
    0 讨论(0)
  • 2020-12-07 15:26

    I had this happen because my tests were not in the same package as my components. (I had renamed my component package, but not my test package.) And I was using @ComponentScan in my test @Configuration class, so my tests weren't finding the components on which they relied.

    So, double check that if you get this error.

    0 讨论(0)
提交回复
热议问题