JDO(datanucleus)单元测试在Eclipse中运行良好但在Maven中失败

时间:2014-12-10 13:15:56

标签: eclipse maven jdo datanucleus unit-testing

我有两个maven项目,它们共享一个共同的父项目(对于公共依赖项等)。第一个(P1)是我保留业务模式的地方。另一个(P2)是一个服务层,它使用P1中的模型和持久性功能。

P1 pom:

<parent>
    <groupId>com.mycompany</groupId>
    <artifactId>common</artifactId>
    <version>1.0.0</version>
</parent>
<artifactId>P1</artifactId>
<version>0.0.1-SNAPSHOT</version>
...

P2 pom:

<parent>
    <groupId>com.mycompany</groupId>
    <artifactId>common</artifactId>
    <version>1.0.0</version>
</parent>
<artifactId>P2</artifactId>
<version>0.0.1-SNAPSHOT</version>

...

<dependencies>
    <dependency>
        <groupId>com.mycompany</groupId>
        <artifactId>P1</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
...

当我在Eclipse中运行P1的单元测试(运行为... Junit测试)或在终端(mvn清洁测试)中,它们都工作得很好。但是,当我运行P2的单元测试时,它们只能在Eclipse中运行(Run as ... Junit test)。使用Maven时,P2中使用P1模型的测试失败。

我检查了类路径,只有两个区别是

  • 在Eclipse中,P1类的路径是P1目录中的目标文件夹。在Maven中,路径是我的本地存储库(应该没问题,因为P1安装在那里)

  • 在Eclipse中,类路径包含对Java开发工具JUnit运行时支持的引用

我已确认P1 / target中的文件与.m2 /.../P1.jar中的文件匹配 - 这包括META-INF / persistence.xml。

我在单元测试中得到的错误是相同的:

javax.jdo.JDODataStoreException: Cannot set long parameter: value = 1 for column "JOINTABLE_A_B.B_ID" : Invalid argument in JDBC call: parameter index out of range: 1
    at org.datanucleus.api.jdo.NucleusJDOHelper.getJDOExceptionForNucleusException(NucleusJDOHelper.java:546)
    at org.datanucleus.api.jdo.JDOTransaction.commit(JDOTransaction.java:171)
    at com.mycompany.dao.TransactionalWork.execute(TransactionalWork.java:19)
    at com.mycompany.dao.BasicDAO.save(BasicDAO.java:40)
    at com.mycompany.service.ServiceHandler.handle(ServiceHandler.java:28)
    at com.mycompany.service.ServiceHandler.handle(ServiceHandler.java:15)
    at com.mycompany.service.ServiceHandlerTest.handling(ServiceHandlerTest.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
NestedThrowablesStackTrace:
java.sql.SQLException: Invalid argument in JDBC call: parameter index out of range: 1
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.sqlException(Unknown Source)
    at org.hsqldb.jdbc.JDBCUtil.outOfRangeArgument(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.checkSetParameterIndex(Unknown Source)
    at org.hsqldb.jdbc.JDBCPreparedStatement.setLong(Unknown Source)
    at org.datanucleus.store.rdbms.datasource.dbcp.DelegatingPreparedStatement.setLong(DelegatingPreparedStatement.java:121)
    at org.datanucleus.store.rdbms.datasource.dbcp.DelegatingPreparedStatement.setLong(DelegatingPreparedStatement.java:121)
    at org.datanucleus.store.rdbms.ParamLoggingPreparedStatement.setLong(ParamLoggingPreparedStatement.java:777)
    at org.datanucleus.store.rdbms.mapping.datastore.BigIntRDBMSMapping.setLong(BigIntRDBMSMapping.java:125)
    at org.datanucleus.store.rdbms.mapping.java.SingleFieldMapping.setLong(SingleFieldMapping.java:150)
    at org.datanucleus.store.rdbms.mapping.AppIDObjectIdFieldManager.storeLongField(AppIDObjectIdFieldManager.java:197)
    at org.datanucleus.state.AppIdObjectIdFieldConsumer.storeLongField(AppIdObjectIdFieldConsumer.java:72)
    at com.mycompany.model.Entity.dnCopyKeyFieldsFromObjectId(Entity.java)
    at org.datanucleus.api.jdo.JDOAdapter.copyKeyFieldsFromIdToObject(JDOAdapter.java:699)
    at org.datanucleus.store.rdbms.mapping.java.PersistableMapping.setObjectAsValue(PersistableMapping.java:648)
    at org.datanucleus.store.rdbms.mapping.java.PersistableMapping.setObject(PersistableMapping.java:323)
    at org.datanucleus.store.rdbms.mapping.java.PersistableMapping.setObject(PersistableMapping.java:302)
    at org.datanucleus.store.rdbms.scostore.JoinSetStore.iterator(JoinSetStore.java:906)
    at org.datanucleus.store.types.wrappers.backed.Set.loadFromStore(Set.java:320)
    at org.datanucleus.store.types.wrappers.backed.Set.iterator(Set.java:485)
    at org.datanucleus.store.fieldmanager.LoadFieldManager.internalFetchObjectField(LoadFieldManager.java:102)
    at org.datanucleus.store.fieldmanager.AbstractFetchDepthFieldManager.fetchObjectField(AbstractFetchDepthFieldManager.java:105)
    at org.datanucleus.state.AbstractStateManager.replacingObjectField(AbstractStateManager.java:1590)
    at org.datanucleus.state.StateManagerImpl.replacingObjectField(StateManagerImpl.java:117)
    at com.mycompany.model.Profile.dnReplaceField(Profile.java)
    at com.mycompany.model.Entity.dnReplaceFields(Entity.java)
    at org.datanucleus.state.StateManagerImpl.replaceFields(StateManagerImpl.java:3131)
    at org.datanucleus.state.StateManagerImpl.replaceFields(StateManagerImpl.java:3158)
    at org.datanucleus.state.AbstractStateManager.loadFieldsInFetchPlan(AbstractStateManager.java:1096)
    at org.datanucleus.ExecutionContextImpl.performDetachAllOnTxnEndPreparation(ExecutionContextImpl.java:4460)
    at org.datanucleus.ExecutionContextImpl.preCommit(ExecutionContextImpl.java:4115)
    at org.datanucleus.ExecutionContextImpl.transactionPreCommit(ExecutionContextImpl.java:683)
    at org.datanucleus.TransactionImpl.internalPreCommit(TransactionImpl.java:388)
    at org.datanucleus.TransactionImpl.commit(TransactionImpl.java:277)
    at org.datanucleus.api.jdo.JDOTransaction.commit(JDOTransaction.java:107)
    at com.mycompany.dao.TransactionalWork.execute(TransactionalWork.java:19)
    at com.mycompany.dao.BasicDAO.save(BasicDAO.java:40)
    at com.mycompany.service.ServiceHandler.handle(ServiceHandler.java:28)
    at com.mycompany.service.ServiceHandler.handle(ServiceHandler.java:15)
    at com.mycompany.service.ServiceHandlerTest.handling(ServiceHandlerTest.java:50)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: org.hsqldb.HsqlException: Invalid argument in JDBC call: parameter index out of range: 1
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    ... 69 more

我使用HyperSQL DB进行单元测试。

有没有人遇到过这个问题并提出解决方案?

1 个答案:

答案 0 :(得分:0)

事实证明,由于我没有在persistence.xml中声明我的类,因此Datanucleus正在扫描类路径。这在运行P1时在Maven中运行良好,因为类不会存放在jar文件中。但是在P2中,即使jar文件在类路径上,扫描也没有成功找到类。 Eclipse中不是这种情况,因为对P1的引用被认为是项目引用 - 因此类不会放在jar文件中。