Embeddable 컨테이너를 사용할 때 My EntityManager는 null을 유지합니다. 동일한 코드는 일반 웹 응용 프로그램에서 호출 할 때 잘 작동합니다.JUnit 테스트 중에 WebSphere embeddable container에 의해 인스턴스화 된 EJB 모듈에 EntityManager가 삽입되지 않았습니다.
시험 번호 :
package com.learning.business.car;
import static org.junit.Assert.assertEquals;
import java.util.List;
import javax.ejb.embeddable.EJBContainer;
import javax.naming.NamingException;
import org.junit.Test;
public class CarRepoTest {
@Test
public void testGetCarsByEjb() throws NamingException {
EJBContainer ec = EJBContainer.createEJBContainer();
CarRepo carRepo = (CarRepo) ec
.getContext()
.lookup("java:global/classes/CarRepo");
List<Car> cars = carRepo.getAll();
assertEquals(0, cars.size());
ec.close();
}
}
상기 코드를 디버깅하는 동안, ec.toString()
날 보여준다
svContainerActive = true
svNamingInitialized = true
ivContext = [email protected][[email protected][super=com.ibm.ws.naming.urlbase.UrlContextImpl(java:)@fe720e63[_schemeId=java, _primaryLeafName=null, _strCtxID=ROOT CONTEXT, [email protected][_nameSpaceName=java:comp(null, null, null), _nameSpaceID=2, _nameSpaceType=1, [email protected][size=2], [email protected][size=2]]], _javaNameSpaceScope=COMPONENT SCOPE, _jns=javaNameSpaceImpl:{_appName=null,_moduleName=null,_componentName=null,_bootstrapAddress=rir:,_clientMode=ISOLATED,_componentNameSpaceLocation=LOCAL,_otherModuleNames=[],_applicationsRootProviderURL=corbaname:rir:/NameServiceApplicationsRoot#,_applicationRootProviderURL=null,_moduleRootProviderURL=null,_componentRootProviderURL=null,[email protected][_nameSpaceName=java:comp(null, null, null), _nameSpaceID=2, _nameSpaceType=1, [email protected][size=2], [email protected][size=2]]}], _caching=false, [email protected]]
ivIsClosed = false
ivModules = [com.[email protected]9202003a[name=classes, file=C:\WorkSpaceRTC\javaee6\target\classes, [email protected][embeddable#classes]]]
ivProperties = {enableProtocolSecurity=false}
ivServiceTokens = [[email protected]:interface com.ibm.ws.runtime.service.MetaDataFactoryMgr, [email protected]:interface com.ibm.wsspi.injectionengine.InjectionEngine]
EJB 모듈 :
package com.learning.business.car;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
@Stateless
public class CarRepo {
@PersistenceContext
protected EntityManager em;
public List<Car> getAll() {
// "em" keeps null and the below line throws a NullPointerException
TypedQuery<Car> query = em.createQuery("SELECT c FROM Car c", Car.class);
return query.getResultList();
}
}
의 persistence.xml :
<?xml version="1.0" encoding="UTF-8" ?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence
http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd"
version="2.0">
<persistence-unit name="javaee6" transaction-type="JTA">
<class>com.learning.business.car.Car</class>
<properties>
<property name="javax.persistence.jdbc.driver" value="org.h2.Driver" />
<property name="javax.persistence.jdbc.url" value="jdbc:h2:mem:test;DB_CLOSE_DELAY=-1" />
<property name="openjpa.jdbc.SynchronizeMappings" value="buildSchema(ForeignKeys=true)"/>
</properties>
</persistence-unit>
</persistence>
예외 :은 WebSphere 응용 프로그램에서 XHTML 페이지에서 소비하면 다른 한편으로
CNTR0020E: EJB threw an unexpected (non-declared) exception during invocation of method "getAll" on bean "BeanId(embeddable#classes#CarRepo, null)". Exception data: java.lang.NullPointerException
at com.learning.business.car.CarRepo.getAll(CarRepo.java:18)
at com.learning.business.car.EJSLocalNSLCarRepo_a1ab5865.getAll(EJSLocalNSLCarRepo_a1ab5865.java)
at com.learning.business.car.CarRepoTest.testGetCarsByEjb(CarRepoTest.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:60)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:37)
at java.lang.reflect.Method.invoke(Method.java:611)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:73)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:46)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:180)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:41)
at org.junit.runners.ParentRunner$1.evaluate(ParentRunner.java:173)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:220)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:46)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
은, 아래의 코드는 작동 :
package com.learning.app.car;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import com.learning.business.car.CarRepo;
@ManagedBean
public class CarBean {
@EJB
public CarRepo carRepo;
public Integer getCarsCount() {
// in a WebSphere application it works as expected (EntityManager is correctly injected in CarRepo)
return carRepo.getAll().size();
}
}
때 JPA 씬 클라이언트를 포함하는 것이 좋습니다
안녕하세요, 다시 하셨어요. 나의 날을 구했다! –