I am trying to auto-wire a spring managed bean to use in my unit test case. But autowired bean is always null. Below are my setting.
my unit test class
@RunWith(MockitoJUnitRunner.class)
@ContextConfiguration(locations = "classpath*:business-context-test.xml")
public class SMSGatewayTest {
@Autowired
@Qualifier("mySMSImpl")
private ISMSGateway smsGateway;
@Test
public void testSendTextMessage() throws Exception {
smsGateway.sendText(new TextMessage("TEST"));
// ^___________ this is null, even though I have set ContextConfiguration
}
}
spring managed bean
package com.myproject.business;
@Component("mySMSImpl")
public class MySMSImpl implements ISMSGateway {
@Override
public Boolean sendText(TextMessage textMessage ) throws VtsException {
//do something
}
}
context for unit test case
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.myproject.business"/>
</beans>
I have seen many questions and all are giving the answers that I've already incorporated in my code. Can some one tell me what I am missing. I am using intellij 14 to run my test case.