java - NoInitialContextFound Exception while running testcases using junit/spring-test to test a spring WS webservice -
i have soap web service developed using spring ws. want create junit test cases test service before moves other environments ( dev sit uat prod).
i trying use spring test's mockwebserviceclient
this
@runwith(springjunit4classrunner.class) @contextconfiguration(locations = { "classpath:web-inf/spring-ws-servlet.xml" }) public class accountsearchendpointtests { @autowired private applicationcontext applicationcontext; private mockwebserviceclient mockclient; @before public void createclient() { mockclient = mockwebserviceclient.createclient(applicationcontext); } @test public void testwsendpointtest() throws exception { stringsource requestpayload = new stringsource( ""); //omitted actual request stringsource responsepayload = new stringsource(""); //omitted actual response mockclient.sendrequest(withpayload(requestpayload)).andexpect(payload(responsepayload)); }
however error
javax.naming.noinitialcontextexception: need specify class name in environment or system property, or applet parameter, or in application resource file: java.naming.factory.initial java.lang.illegalstateexception: failed load applicationcontext @ org.springframework.test.context.testcontext.getapplicationcontext(testcontext.java:308) @ org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.injectdependencies(dependencyinjectiontestexecutionlistener.java:109) @ org.springframework.test.context.support.dependencyinjectiontestexecutionlistener.preparetestinstance(dependencyinjectiontestexecutionlistener.java:75) @ org.springframework.test.context.testcontextmanager.preparetestinstance(testcontextmanager.java:321) @ org.springframework.test.context.junit4.springjunit4classrunner.createtest(springjunit4classrunner.java:220) @ org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:301) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:303) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:240) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:57) @ org.junit.runners.parentrunner$3.run(parentrunner.java:290) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:71) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:288) @ org.junit.runners.parentrunner.access$000(parentrunner.java:58) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:268) @ org.springframework.test.context.junit4.statements.runbeforetestclasscallbacks.evaluate(runbeforetestclasscallbacks.java:61) @ org.springframework.test.context.junit4.statements.runaftertestclasscallbacks.evaluate(runaftertestclasscallbacks.java:70) @ org.junit.runners.parentrunner.run(parentrunner.java:363) @ org.springframework.test.context.junit4.springjunit4classrunner.run(springjunit4classrunner.java:180) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:467) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:683) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:390) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:197) caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'bean' defined in class path resource [web-inf/spring-ws-servlet.xml]: invocation of init method failed; nested exception javax.naming.noinitialcontextexception: need specify class name in environment or system property, or applet parameter, or in application resource file: java.naming.factory.initial @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1455) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:519) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:456) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:294) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:225) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:291) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:193) @ org.springframework.beans.factory.support.defaultlistablebeanfactory.preinstantiatesingletons(defaultlistablebeanfactory.java:585) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:913) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:464) @ org.springframework.test.context.support.abstractgenericcontextloader.loadcontext(abstractgenericcontextloader.java:84) @ org.springframework.test.context.support.abstractgenericcontextloader.loadcontext(abstractgenericcontextloader.java:1) @ org.springframework.test.context.testcontext.loadapplicationcontext(testcontext.java:280) @ org.springframework.test.context.testcontext.getapplicationcontext(testcontext.java:304) ... 24 more caused by: javax.naming.noinitialcontextexception: need specify class name in environment or system property, or applet parameter, or in application resource file: java.naming.factory.initial @ javax.naming.spi.namingmanager.getinitialcontext(namingmanager.java:662) @ javax.naming.initialcontext.getdefaultinitctx(initialcontext.java:313) @ javax.naming.initialcontext.geturlordefaultinitctx(initialcontext.java:350) @ javax.naming.initialcontext.lookup(initialcontext.java:417) @ com.bean.initialize(bean.java:68) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:62) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:497) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.invokecustominitmethod(abstractautowirecapablebeanfactory.java:1581) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.invokeinitmethods(abstractautowirecapablebeanfactory.java:1522) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1452) ... 37 more
this bean in spring-ws-servlet
<bean id="bean" init-method="initialize" destroy-method="terminate" class="com.bean" />
and in initialize of bean happens
ctx = new initialcontext(); ds = (datasource) ctx.lookup(ds_name);
i understand webservice should running in container tomcat, want know how can mock it/ configure in spring test it. want know how can go testing webservice using junit!
this duplicate of https://stackoverflow.com/a/5947016/388980.
so check out solutions discussed there.
in summary, should not performing jdni look-ups directly within code. instead, should externalize jdni lookup in spring configuration , have spring inject datasource
components.
you can use embedded database (e.g., h2, hsql, derby -- spring supports 3 of those) while testing , inject datasource
embedded database components.
as alternative, move data access code dedicated repository or dao implementation , inject mock web service endpoints -- example, using mockito.
if you're not familiar bean definition profiles in spring, should read corresponding sections of spring reference manual. testing chapter demonstrates how use profiles prod
production , dev
in tests.
Comments
Post a Comment