【发布时间】:2011-08-04 00:13:15
【问题描述】:
在运行 jUnit 测试时,我在实例化 weblogic 初始上下文时遇到了大问题。 应用程序使用 Spring/hibernate/weblogic。 在应用程序代码中,方法使用 JMS 代理向 JMS 发送消息,队列在 weblogic 上设置。
我的问题是,目前 JUnit 测试时,我需要让 weblogic 服务器在本地机器上运行,只是为了初始化在 JMS 代理中使用的 WeblogicInitialContext。我的 junit 测试不需要向服务器发送任何东西,没有 JMS ,没有数据源。所有都由弹簧单元测试框架处理。 我想为我的 junit 测试解耦/摆脱 Weblogic。请建议。 这是我的代码:
这是我的测试应用程序上下文 XML:
<beans xmlns="http://www.springframework.org/schema/beans"
...
>
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<util:properties id="webLogicInitialContext">
<prop key="java.naming.factory.initial">weblogic.jndi.WLInitialContextFactory</prop>
<prop key="java.naming.provider.url">t3://localhost:7001</prop>
<prop key="java.naming.security.principal">admin</prop>
<prop key="java.naming.security.credentials">password</prop>
</util:properties>
<jee:jndi-lookup id="responseProxyConnectionFactory"
jndi-name="jms/ConnectionFactory" environment-ref="webLogicInitialContext"/>
<bean id="responseProxyJmsTemplate" class="org.springframework.jms.core.JmsTemplate">
<property name="connectionFactory"
ref="responseProxyConnectionFactory" />
</bean>
</beans>
这是一种 Java 类方法:
public class Order {
public void addOrder(OrderRequest addOrderRequest) {
PurchaseOrder newOrder = orderHelper.createOrder(addOrderRequest);
orderDaoHibernate.addOrder(newOrder);
responseProxy.send(newOrder);
}
}
我的测试:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners( {TransactionalTestExecutionListener.class, DependencyInjectionTestExecutionListener.class,DirtiesContextTestExecutionListener.class })
@ContextConfiguration(locations={"/test-application-context.xml"})
@TransactionConfiguration(defaultRollback=true)
public class TestOrder {
@Test
@Transactional
public void testMyOrder(){
Order ord = new Order();
OrderRequest req = new OrderRequest();
....
ord.addOrder(req);
}
}
如果我运行这个测试,Spring 框架会尝试加载 WeblogicInitialContext ,如果本地 weblogic 没有运行它会抛出异常。
当我从 Junit 调用此方法时,我不想发送任何 JMS 消息。 如何创建虚拟 WeblogicInitialContext。
请帮忙。
异常的一部分;
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'responseProxyConnectionFactory': Invocation of init method failed; nested exception is javax.naming.CommunicationException [Root exception is java.net.ConnectException: t3://localhost:7001: Destination unreachable; nested exception is:
【问题讨论】:
标签: hibernate spring weblogic junit4