我有一个简单的Jms类,我想使用Spring-test和Junit进行测试。
现在,主应用程序使用Jms将消息发布到Queue。我有一个特定的jms-context.xml,其中包含此JmsTemplate的所有配置:
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:property-placeholder location="file:env/dev/conf/ingest.properties" ignore-unresolvable="true"/>
<!-- Tibco EMS configuration -->
<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
<property name="environment">
<props>
<prop key="java.naming.factory.initial">com.tibco.tibjms.naming.TibjmsInitialContextFactory</prop>
<prop key="java.naming.provider.url">${tibco.ems.provider.url}</prop>
<prop key="java.naming.security.principal">${tibco.ems.username}</prop>
<prop key="java.naming.security.credentials">${tibco.ems.password}</prop>
</props>
</property>
</bean>
<bean id="emsConnectionFactory" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate" />
<property name="jndiName" value="${tibco.ems.TopicConnectionFactoryName}" />
</bean>
<bean id="userCredentialsConnectionFactory" class="org.springframework.jms.connection.UserCredentialsConnectionFactoryAdapter">
<property name="targetConnectionFactory">
<ref bean="emsConnectionFactory" />
</property>
<property name="username" value="${tibco.ems.username}" />
<property name="password" value="${tibco.ems.password}" />
</bean>
<!-- EMS Enterprise topic publisher -->
<bean id="jmsTopicPublisher" class="com.scrippsnetworks.ingest.common.message.jms.JmsTopicPublisher">
<property name="jmsTemplate" ref="jmsTemplate" />
</bean>
<!-- EMS Sender Destination -->
<bean id="enterpriseEventDestination" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiTemplate" ref="jndiTemplate"/>
<property name="jndiName" value="${asset.topic}"/>
</bean>
<!-- EMS Template for enterprise event -->
<bean id="jmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory" ref="userCredentialsConnectionFactory"/>
<property name="defaultDestination" ref="enterpriseEventDestination"/>
</bean>
在集成测试中,它工作正常,但对于我的单元测试,我使用了一个junit类,这个:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:jms-event-generator-test.xml")
public class JmsEventGeneratorTest {
@Mock
JmsTopicPublisher publisher;
@Autowired
@InjectMocks
EventGenerator messageGenerator;
@Before
public void init() throws Exception {
MockitoAnnotations.initMocks(this);
}
@Test
public void bootstrapTest() {
Assert.assertNotNull(messageGenerator);
}
@Test
public void testMessageAsset() {
SarAssetType asset = new SarAssetType();
asset.setScrid("1234");
asset.setType("TOTO");
messageGenerator.generateAssetCreationMessage(asset);
}
jms-context-test文件如下所示:
<?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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd">
<context:property-placeholder location="file:env/dev/conf/ingest.properties"/>
<context:component-scan base-package="com.xxx.ingest.common.message"/>
<bean id="eventHelper" class="com.xxx.common.event.EventHelper"/>
<bean id="jmsTemplate" class="org.xxx.jms.core.JmsTemplate"/>
因此,当我在上面运行我的junit测试时,它失败了,因为它无法连接到tibco服务器!!!!
我注意到它是因为我远程工作并且从vpn中被连接起来。
那么为什么Spring尝试使用真正的tibco东西加载主应用程序上下文文件?
此致