当前位置: 首页 > 知识库问答 >
问题:

运行依赖于WebSphere中定义的资源的JUnit测试

余信然
2023-03-14
2013-07-31 13:46:17,008:could not load the application context
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [myApplication.xml]: Invocation of init method failed; nested exception is javax.naming.ServiceUnavailableException: Could not obtain an initial context due to a communication failure. Since no provider URL was specified, the default provider URL of "corbaloc:iiop:1.0@myMachineName.myCompany.com:2809/NameService" was used.  Make sure that any bootstrap address information in the URL is correct and that the target name server is running.  Possible causes other than an incorrect bootstrap address or unavailable name server include the network environment and workstation network configuration. [Root exception is org.omg.CORBA.TRANSIENT: java.net.ConnectException: Connection refused: connect:host=myMachineName.myCompany.com,port=2809  vmcid: IBM  minor code: E02  completed: No]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1338)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
    at java.security.AccessController.doPrivileged(AccessController.java:224)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)

DataSourcebean的定义很简单:

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
    <property name="jndiName" value="jdbc/dataSource" />
</bean>

如何使Spring正确地连接到WebSphere JNDI,即使单元测试是在应用程序容器之外运行的?

共有1个答案

帅银龙
2023-03-14

尽管没有连接到WebSphere实例,但在查看JNDI资源时,您始终可以使用org.springframework.mock.JNDI包中的SimpleNamingContextBuilder。这允许您构建一个对象(例如,使用自己的直接绑定到远程JNDI服务的数据源),然后将其绑定到一个“模拟”JNDI服务,以便在测试时注入到Spring应用程序上下文中。

为此,您需要在JUnit测试的@BeForeClass(static)中进行,以确保在应用程序上下文启动之前JNDI绑定可用。(这样应用程序上下文可以在查找jdbc/datasource时找到一些东西)

如果您要在持续集成环境中使用此方法,我不建议您连接另一台服务器,但是如果您只想进行“一次性的手动测试”,您可以尝试以下方法;

@BeforeClass
public static void beforeClass() throws Exception {

    Properties properties = new Properties();
    //build your properties to the remove class
    Context context = new InitialContext(properties);
    //look up your dataSource
    DataSource ds = (DataSource) context.lookup("");
    //now build the simple
    SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
    builder.bind("jdbc/dataSource", ds);

}
 类似资料:
  • Remove wrapper, open mouth, insert muffin, eat. — Instructions on 7-11 muffin packaging 为确保事物以正确的顺序发生,你可以在 Puppet 中指定一个资源依赖另一个资源, 例如:你必须先安装软件包 X 然后再启动它提供的服务,因此应该标记这项服务依赖于软件包 X。 Puppet 会按要求的顺序排出它遇到的所有依

  • 我正在开发一组Java/Sprint引导Web服务,这些服务都将有几个相同的(理想情况下是可重用的)基于Spring的资源: 许多基于注释的Spring Security配置 多个服务和组件 许多基于注释的事件处理程序 基于注释的异常处理程序 扩展ResponseEntityExceptionHandler的ControllerAdvice 理想情况下,我可以将它们放在一个库(共享JAR文件)中,

  • 我试图从Wildfly9连接到Websphere MQ,为此我添加了一个独立的资源适配器-full.xml 我已经添加了配置属性并删除了它们添加了不同的属性,这些属性看起来可能很有用,比如use-java-context=“true”pool-name=“jmsConnectionFactory”use-ccm=“true”删除了它们或使它们为false,更改了jndi名称,但它一直给我相同的结果

  • 4.6 资源依赖 如果bean本身将通过某种动态过程来确定和提供资源路径,那么bean可以使用ResourceLoader接口来加载资源。 j假设以某种方式加载一个模板,其中需要的特定资源取决于用户的角色。 如果资源是静态的,那么完全消除ResourceLoader接口的使用是有意义的,只需让bean公开它需要的Resource属性,那么它们就会以你所期望的方式被注入。 什么使得它们轻松注入这些属

  • 问题内容: 我们的工具包具有15000多个JUnit测试,并且已知许多测试会在其他测试失败的情况下失败。例如,如果方法X.foo()使用Y.bar()中的功能并且YTest.testBar()失败,则XTest.testFoo()也将失败。显然,由于特定于X.foo()的问题,XTest.testFoo()也可能会失败。 尽管这很好并且我仍然希望两个测试都能运行,但是如果可以用指向XTest.te

  • 对于 REST API 的测试部分。正在查看可以在Spring启动中使用的依赖项。我看到有JUnit依赖项和Spring启动器测试依赖项。 JUnit依赖项和Spring Boot启动器测试依赖项有什么区别?