java - Configuring data access layer properties files in Spring Integeration testing environment -
i setting test environment spring web application. use .properties
files , environment
configure persistence layer properties shown below(i have skipped code brevity):
persistenceconfig.java
@propertysources({ @propertysource(value= "classpath:/com/hp/properties/persistence/mysqldb.properties"), @propertysource(value = "classpath:/com/hp/properties/persistence/hibernate.properties")}) public class persistenceconfig { @autowired environment env; @bean public localcontainerentitymanagerfactorybean entitymanagerfactory(){ localcontainerentitymanagerfactorybean em = new localcontainerentitymanagerfactorybean(); em.setdatasource(datasource()); em.setpackagestoscan(new string[] {"com.hp.model.entity"}); jpavendoradapter vendoradapter = new hibernatejpavendoradapter(); em.setjpavendoradapter(vendoradapter); em.setjpaproperties(hibernateproperties()); return em; } private properties hibernateproperties(){ properties properties = new properties(); properties.setproperty("hibernate.dialect", env.getrequiredproperty("hibernate.dialect.mysql")); properties.setproperty("hibernate.show_sql", env.getrequiredproperty("hibernate.showsql")); properties.setproperty("hibernate.format_sql", env.getrequiredproperty("hibernate.formatsql")); return properties; } }
i creating test classes using spring's webapplicationcontext
shown below:
@runwith(springjunit4classrunner.class) @contextconfiguration(classes={applicationconfig.class}) @webappconfiguration @testpropertysource(value = {"classpath:/com/hp/properties/persistence/mysqldb.properties", "classpath:/com/hp/properties/persistence/hibernate.properties"}) public class patientservicetest { mockmvc mockmvc; @autowired webapplicationcontext webapplicationcontext; @before public void setup(){ mockmvc = mockmvcbuilders.webappcontextsetup(webapplicationcontext).build(); } @test public void testfordependentbeans(){ assert.assertthat(webapplicationcontext.containsbean("patientrepository"), matchers.is(true)); }
}
but run test error intialization of persistnceconfig
hibernate.properties
not found. have tried copying these files src/main/java
src/test/java
, add @testpropertysource
explicitly provide properties file. yet getting same error. below stack trace if helps.
also, how can load different properties file specific test environment in spring integeration testing used production.
java.lang.illegalstateexception: failed load applicationcontext @ org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:124) @ org.springframework.test.context.support.defaulttestcontext.getapplicationcontext(defaulttestcontext.java:83) @ org.springframework.test.context.web.servlettestexecutionlistener.setuprequestcontextifnecessary(servlettestexecutionlistener.java:189) @ org.springframework.test.context.web.servlettestexecutionlistener.preparetestinstance(servlettestexecutionlistener.java:131) @ org.springframework.test.context.testcontextmanager.preparetestinstance(testcontextmanager.java:230) @ org.springframework.test.context.junit4.springjunit4classrunner.createtest(springjunit4classrunner.java:228) @ org.springframework.test.context.junit4.springjunit4classrunner$1.runreflectivecall(springjunit4classrunner.java:287) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:12) @ org.springframework.test.context.junit4.springjunit4classrunner.methodblock(springjunit4classrunner.java:289) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:247) @ org.springframework.test.context.junit4.springjunit4classrunner.runchild(springjunit4classrunner.java:94) @ 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:191) @ 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:459) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:675) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:382) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:192) caused by: org.springframework.beans.factory.beancreationexception: error creating bean name 'entitymanagerfactory' defined in class path resource [com/hp/config/persistenceconfig.class]: invocation of init method failed; nested exception java.lang.nosuchmethoderror: javax.persistence.spi.persistenceunitinfo.getvalidationmode()ljavax/persistence/validationmode; @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.initializebean(abstractautowirecapablebeanfactory.java:1583) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.docreatebean(abstractautowirecapablebeanfactory.java:545) @ org.springframework.beans.factory.support.abstractautowirecapablebeanfactory.createbean(abstractautowirecapablebeanfactory.java:482) @ org.springframework.beans.factory.support.abstractbeanfactory$1.getobject(abstractbeanfactory.java:306) @ org.springframework.beans.factory.support.defaultsingletonbeanregistry.getsingleton(defaultsingletonbeanregistry.java:230) @ org.springframework.beans.factory.support.abstractbeanfactory.dogetbean(abstractbeanfactory.java:302) @ org.springframework.beans.factory.support.abstractbeanfactory.getbean(abstractbeanfactory.java:197) @ org.springframework.context.support.abstractapplicationcontext.getbean(abstractapplicationcontext.java:1076) @ org.springframework.context.support.abstractapplicationcontext.finishbeanfactoryinitialization(abstractapplicationcontext.java:851) @ org.springframework.context.support.abstractapplicationcontext.refresh(abstractapplicationcontext.java:541) @ org.springframework.test.context.web.abstractgenericwebcontextloader.loadcontext(abstractgenericwebcontextloader.java:134) @ org.springframework.test.context.web.abstractgenericwebcontextloader.loadcontext(abstractgenericwebcontextloader.java:61) @ org.springframework.test.context.support.abstractdelegatingsmartcontextloader.delegateloading(abstractdelegatingsmartcontextloader.java:108) @ org.springframework.test.context.support.abstractdelegatingsmartcontextloader.loadcontext(abstractdelegatingsmartcontextloader.java:251) @ org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontextinternal(defaultcacheawarecontextloaderdelegate.java:98) @ org.springframework.test.context.cache.defaultcacheawarecontextloaderdelegate.loadcontext(defaultcacheawarecontextloaderdelegate.java:116) ... 25 more caused by: java.lang.nosuchmethoderror: javax.persistence.spi.persistenceunitinfo.getvalidationmode()ljavax/persistence/validationmode;
Comments
Post a Comment