我正在进行JUnit测试,并且我的堆栈跟踪中出现了一些我无法理解的错误。我也不确定我用于OpenEJB和Hibernate的版本。
我的JUnit:
public class UsersDaoTest {
@EJB
private UsersDao usersDao;
@Test
public void testPersist() {
UsersBean usersBean = TrialUtil.generateUser(usersDao, "one", "john", "password", "john", "smith", "james");
assertNotNull(usersBean);
}
@Test
public void testIsUserRegistered() {
TrialUtil.generateUser(usersDao, "one", "john", "password", "john", "smith", "james");
assertNotNull(usersDao.isUserRegistered("john", "password"));
}
private static EJBContainer container;
@BeforeClass
public static void start() {
container = TrialUtil.createContainer();
}
@Before
public void inject() throws NamingException {
container.getContext().bind("inject", this);
}
@AfterClass
public static void stop() {
container.close();
} }
虽然这是我的TrialUtil类:
public class TrialtUtil {
public static UsersBean generateUser(UsersDao usersDao,
String userId, String username, String password, String firstName,
String lastName, String middleName) {
UsersBean usersBean = new UsersBean();
usersBean .setUserId(userId);
usersBean .setUsername(username);
usersBean .setPassword(password);
usersBean .setFirstName(firstName);
usersBean .setLastName(lastName);
usersBean .setMiddleName(middleName);
usersDao .makePersistent(usersBean);
return usersBean;
}
public static EJBContainer createContainer() {
// TODO Auto-generated method stub
final Properties p = new Properties();
// HSQL
p.put("SampleDataSource", "new://Resource?type=DataSource");
p.put("SampleDataSource.JdbcDriver", "org.hsqldb.jdbcDriver");
p.put("SampleDataSource.JdbcUrl", "jdbc:hsqldb:mem:mem:aname");
p.put("SampleDataSource.UserName", "sa");
return EJBContainer.createEJBContainer(p);
} }
的persistence.xml:
<persistence version="2.0" 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">
<persistence-unit name="SampleApiPersistenceUnit" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>SampleDataSource</jta-data-source>
<class>com.test.rest.sample.entity.testdb.MstMusicBean</class>
<properties>
<property name="hibernate.dialect" value="org.hibernate.dialect.HSQLDialect" />
<property name="hibernate.hbm2ddl.auto" value="create" />
</properties>
</persistence-unit>
所以这是我的堆栈跟踪(抱歉,我无法正确格式化。):
org.apache.openejb.OpenEjbContainer$AssembleApplicationException: org.apache.openejb.OpenEJBException: Creating application failed: C:\Users\John\Documents\API\SampleApi: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:396)
at javax.ejb.embeddable.EJBContainer.createEJBContainer(EJBContainer.java:56)
at com.example.rest.dao.sampledb.TrialUtil.createContainer(TestUtil.java:42)
at com.example.rest.dao.sampledb.UsersDaoTest.start(UsersDaoTest.java:55)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.RunBefores.evaluate(RunBefores.java:27)
at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:31)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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) (Statement after this is supposed to be new line, ignore this)Caused by: org.apache.openejb.OpenEJBException: Creating application failed: C:\Users\John\Documents\API\SampleApi: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:959)
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:537)
at org.apache.openejb.OpenEjbContainer$Provider.createEJBContainer(OpenEjbContainer.java:392)
... 19 more (Statement after this is supposed to be new line, ignore this)Caused by: java.lang.NoSuchMethodError: javax.persistence.spi.PersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
at org.hibernate.ejb.Ejb3Configuration.configure(Ejb3Configuration.java:641)
at org.hibernate.ejb.HibernatePersistence.createContainerEntityManagerFactory(HibernatePersistence.java:91)
at org.apache.openejb.assembler.classic.EntityManagerFactoryCallable.call(EntityManagerFactoryCallable.java:55)
at org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory.createDelegate(ReloadableEntityManagerFactory.java:102)
at org.apache.openejb.assembler.classic.ReloadableEntityManagerFactory.<init>(ReloadableEntityManagerFactory.java:93)
at org.apache.openejb.assembler.classic.PersistenceBuilder.createEntityManagerFactory(PersistenceBuilder.java:150)
at org.apache.openejb.assembler.classic.Assembler.createApplication(Assembler.java:671)
... 21 more (Statement after this is supposed to be new line, ignore this)java.lang.NullPointerException
at com.example.rest.dao.sampledb.UsersDaoTest.stop(MstUsersDaoTest.java:65)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
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.RunAfters.evaluate(RunAfters.java:37)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
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)
因此NullPointerException指向container.close();
行的TrialUtil类。我已经阅读了this和this,但我不太确定我在Eclipse(JavaEE)中使用的是什么版本。如果有人知道这些错误,请帮忙。谢谢。