hibernate - Spring Inject PersistenceContext -
trying implement ddd architecture aspect oriented tests access database , check if user exists, using aspectj ltw...
currently faces 2 issues, don't know if class being injected in spring context. have tried add
//@runwith(springjunit4classrunner.class) //@contextconfiguration(locations = {"classpath*:entitytest-context.xml"})
with no sucess. here test trying run. if notice creating entitymanager on @before don't know if proper usage, because when try find object created returned null.
package ienterprise.common.aspects; import ienterprise.common.model.companyposition; import ienterprise.common.model.internaluser; import org.junit.before; import org.junit.test; import org.junit.runner.runwith; import org.slf4j.logger; import org.slf4j.loggerfactory; import org.springframework.test.context.junit4.springjunit4classrunner; import javax.persistence.entitymanager; import javax.persistence.entitymanagerfactory; import javax.persistence.persistence; import javax.persistence.persistencecontext; import static org.junit.assert.assertequals; import org.springframework.test.context.contextconfiguration; //@runwith(springjunit4classrunner.class) //@contextconfiguration(locations = {"classpath*:entitytest-context.xml"}) public class entitytest { private static final logger logger = loggerfactory.getlogger(entitytest.class); // @persistencecontext(unitname="mysql") // fixme inject in unit tests private static entitymanager manager; @before public void setup(){ entitymanagerfactory mngfactory = persistence.createentitymanagerfactory("mysql"); manager = mngfactory.createentitymanager(); } @test public void createuser(){ internaluser someguy = new internaluser(); someguy.setname("adam"); someguy.setuser("engineer"); someguy.create(); logger.debug("created user: {}", someguy); //fixme: can't find user in database. internaluser founduser = manager.find(internaluser.class, 1l); logger.debug("fetched user: {}",founduser); assertequals( someguy, founduser); } }
our context xml
<?xml version="1.0" encoding="utf-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemalocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <context:load-time-weaver/> <bean id="entitymanagerfactory" class="org.springframework.orm.jpa.localcontainerentitymanagerfactorybean" > <property name="persistencexmllocation" value="classpath*:meta-inf/persistence.xml"></property> </bean> </beans>
how can remove @before , inject persistencecontext?
how can make sure class has spring context?
this new stuff me, , appreciate links github repositories if there kind of spring+hibernate+aspectj+junit setup.
let me know if not clear or additional detail necessary.
Comments
Post a Comment