【发布时间】:2016-09-06 19:00:30
【问题描述】:
我正在尝试自动连接一个 spring 管理的 bean 以在我的单元测试用例中使用。但自动装配的 bean 始终为空。以下是我的设置。
我的单元测试类
@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 托管 bean
package com.myproject.business;
@Component("mySMSImpl")
public class MySMSImpl implements ISMSGateway {
@Override
public Boolean sendText(TextMessage textMessage ) throws VtsException {
//do something
}
}
单元测试用例的上下文
<?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>
我看到了很多问题,并且都给出了我已经包含在我的代码中的答案。有人可以告诉我我错过了什么。我正在使用 intellij 14 运行我的测试用例。
【问题讨论】:
标签: java spring unit-testing intellij-idea junit