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
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.
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.
<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.
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
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"})
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.