簡體   English   中英

運行依賴於WebSphere中定義的資源的JUnit測試

[英]Running JUnit tests that depend on resources defined in WebSphere

我已經編寫了一些單元測試,這些單元測試引用了一些類,這些類引用了其他類,這些類依賴於WebSphere中定義的JDBC資源。 有問題的單元測試實際上並不會觸發任何數據庫讀取或寫入操作,但是它們所引用的類是由Spring創建的,而Spring配置文件試圖將JDBC資源注入其中。 Spring失敗,並顯示以下錯誤消息:

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)

dataSource bean的定義很簡單:

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

即使單元測試在應用程序容器之外運行,如何才能使Spring正確連接到WebSphere JNDI?

盡管沒有連接到WebSphere實例,但是在查看JNDI資源時,您始終可以使用org.springframework.mock.jndi包中的SimpleNamingContextBuilder 這使您可以構建一個對象(例如,具有自己直接綁定到遠程JNDI服務的DataSource),然后將其綁定到“模擬” JNDI服務,以便在測試時注入到Spring Application Context中。

為此,您需要在JUnit測試的@BeforeClass(靜態)中執行此操作,以確保在App上下文啟動之前 ,JNDI綁定可用。 (因此,App上下文在查找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);

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM