java - Running JUnit tests that depend on resources defined in WebSphere -
i've written unit tests reference classes reference other classes have dependencies on jdbc resources defined in websphere. unit tests in question don't trigger database reads or writes, classes refer created spring , spring config file tries inject jdbc resources them. spring fails, error message:
2013-07-31 13:46:17,008:could not load application context org.springframework.beans.factory.beancreationexception: error creating bean name 'datasource' defined in class path resource [myapplication.xml]: invocation of init method failed; nested exception javax.naming.serviceunavailableexception: not obtain initial context due communication failure. since no provider url specified, default provider url of "corbaloc:iiop:1.0@mymachinename.mycompany.com:2809/nameservice" used. make sure bootstrap address information in url correct , target name server running. possible causes other incorrect bootstrap address or unavailable name server include network environment , workstation network configuration. [root exception org.omg.corba.transient: java.net.connectexception: connection refused: connect:host=mymachinename.mycompany.com,port=2809 vmcid: ibm minor code: e02 completed: no] @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1338) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:473) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory$1.run(abstractautowirecapablebeanfactory.java:409) @ java.security.accesscontroller.doprivileged(accesscontroller.java:224) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:380)
the definition of datasource
bean simple:
<bean id="datasource" class="org.springframework.jndi.jndiobjectfactorybean"> <property name="jndiname" value="jdbc/datasource" /> </bean>
how can spring correctly connect websphere jndi when unit test run outside of application container?
though not connecting websphere instance, when looking @ jndi resources, can use simplenamingcontextbuilder
org.springframework.mock.jndi
package. allows build object (say datasource own direct binding remote jndi service) bind 'mock' jndi service injection spring application context @ test time.
to this, you'll need in @beforeclass (static) of junit test ensure jndi bindings available before app context starts up. (so app context can find when looks jdbc/datasource)
i wouldn't recommend connecting server if you're going use in continuous integration environment, if you're looking @ doing 'one-off, manual test' try following;
@beforeclass public static void beforeclass() throws exception { properties properties = new properties(); //build properties remove class context context = new initialcontext(properties); //look datasource datasource ds = (datasource) context.lookup(""); //now build simple simplenamingcontextbuilder builder = simplenamingcontextbuilder.emptyactivatedcontextbuilder(); builder.bind("jdbc/datasource", ds); }
Comments
Post a Comment