从maven执行测试时失败,但不是从Intellij执行

时间:2017-03-08 18:37:56

标签: java maven junit h2 spring-test

我正在使用JUnit,PowerMock,Spring Test和内存H2数据库编写单元测试。当我从Intellij运行测试时,一切都正常运行。但是当我从maven(来自Intellij或来自命令行)运行测试时,它们会失败。

这是我的配置:

   @EnableTransactionManagement
   @EnableJpaRepositories("my.app.repository")
   public class ApplicationTestConfiguration {
      @Bean
      public SimpleDriverDataSource dataSource() {
         SimpleDriverDataSource simpleDriverDataSource = new SimpleDriverDataSource();
         simpleDriverDataSource.setDriverClass(org.h2.Driver.class);
         simpleDriverDataSource.setUrl("jdbc:h2:mem:./ScenarioService;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1");
         simpleDriverDataSource.setUsername("sa");
         simpleDriverDataSource.setPassword("sa");
         return simpleDriverDataSource;
      }
      (...)
      private Properties hibernateProperties() {
         Properties props = new Properties();
         props.setProperty("hibernate.dialect","org.hibernate.dialect.H2Dialect");
         props.setProperty("hibernate.hbm2ddl.auto", "create-drop");
         props.setProperty("hibernate.show_sql","true");
         return props;
      }
   }

这是一个样本测试:

@RunWith(PowerMockRunner.class)
@PowerMockRunnerDelegate(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebAppInitializer.class,ApplicationTestConfiguration.class, DirtiesContextTestExecutionListener.class})
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@DirtiesContext(classMode = DirtiesContext.ClassMode.AFTER_EACH_TEST_METHOD)
public final class ReadTicketsTest {

    @Autowired //this is a spring JPARepository
    protected TestRepository testRepository;

    @Before
    public void init() {
         testRepository.save(engineConfig);
    }

    @Test
    public void ticketShouldBeRead() {
        //assertion code
    }
}

当我从intelliJ运行测试并且它们成功时,在hibernate日志中我看到类似这样的内容:

Hibernate: drop table EngineConfigs if exists
Hibernate: create table EngineConfigs (id binary(16) not null, created datetime(6) DEFAULT NULL, customData MEDIUMTEXT, customerId binary(16) not null, signature varchar(255), signatureVersion integer, updated datetime(6) DEFAULT NULL, advancedConfiguration TEXT, engineConfigName varchar(255) not null, engineServiceName varchar(255) not null, locked boolean, probabilityMaximum integer not null, primary key (id))
Hibernate: select engineconf0_.id as id1_1_1_, engineconf0_.created as created2_1_1_, engineconf0_.customData as customDa3_1_1_, engineconf0_.customerId as customer4_1_1_, engineconf0_.signature as signatur5_1_1_, engineconf0_.signatureVersion as signatur6_1_1_, engineconf0_.updated as updated7_1_1_, engineconf0_.advancedConfiguration as advanced8_1_1_, engineconf0_.engineConfigName as engineCo9_1_1_, engineconf0_.engineServiceName as engineS10_1_1_, engineconf0_.locked as locked11_1_1_, engineconf0_.probabilityMaximum as probabi12_1_1_, accumulato1_.EngineConfigs_id as EngineCo1_1_3_, accumulato2_.id as accumula2_2_3_, accumulato2_.id as id1_0_0_, accumulato2_.created as created2_0_0_, accumulato2_.customData as customDa3_0_0_, accumulato2_.customerId as customer4_0_0_, accumulato2_.signature as signatur5_0_0_, accumulato2_.signatureVersion as signatur6_0_0_, accumulato2_.updated as updated7_0_0_, accumulato2_.accumulatorNumber as accumula8_0_0_, accumulato2_.description as descript9_0_0_, accumulato2_.engineConfig_id as engineC12_0_0_, accumulato2_.numberOfHits as numberO10_0_0_, accumulato2_.prize as prize11_0_0_ from EngineConfigs engineconf0_ left outer join EngineConfigs_Accumulators accumulato1_ on engineconf0_.id=accumulato1_.EngineConfigs_id left outer join Accumulators accumulato2_ on accumulato1_.accumulators_id=accumulato2_.id where engineconf0_.id=?
Hibernate: insert into EngineConfigs (created, customData, customerId, signature, signatureVersion, updated, advancedConfiguration, engineConfigName, engineServiceName, locked, probabilityMaximum, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
Hibernate: drop table EngineConfigs if exists

这是有道理的,因为预期它将创建数据库,执行测试(因此插入),然后由于@DirtiesContextAnnotation而删除所有表。 但是当我从maven运行测试时,在hibernate日志中我看到类似这样的东西:

Hibernate: drop table EngineConfigs if exists
Hibernate: create table EngineConfigs (id binary(16) not null, created datetime(6) DEFAULT NULL, customData MEDIUMTEXT, customerId binary(16) not null, signature varchar(255), signatureVersion integer, updated datetime(6) DEFAULT NULL, advancedConfiguration TEXT, engineConfigName varchar(255) not null, engineServiceName varchar(255) not null, locked boolean, probabilityMaximum integer not null, primary key (id))
Hibernate: select engineconf0_.id as id1_1_1_, engineconf0_.created as created2_1_1_, engineconf0_.customData as customDa3_1_1_, engineconf0_.customerId as customer4_1_1_, engineconf0_.signature as signatur5_1_1_, engineconf0_.signatureVersion as signatur6_1_1_, engineconf0_.updated as updated7_1_1_, engineconf0_.advancedConfiguration as advanced8_1_1_, engineconf0_.engineConfigName as engineCo9_1_1_, engineconf0_.engineServiceName as engineS10_1_1_, engineconf0_.locked as locked11_1_1_, engineconf0_.probabilityMaximum as probabi12_1_1_, accumulato1_.EngineConfigs_id as EngineCo1_1_3_, accumulato2_.id as accumula2_2_3_, accumulato2_.id as id1_0_0_, accumulato2_.created as created2_0_0_, accumulato2_.customData as customDa3_0_0_, accumulato2_.customerId as customer4_0_0_, accumulato2_.signature as signatur5_0_0_, accumulato2_.signatureVersion as signatur6_0_0_, accumulato2_.updated as updated7_0_0_, accumulato2_.accumulatorNumber as accumula8_0_0_, accumulato2_.description as descript9_0_0_, accumulato2_.engineConfig_id as engineC12_0_0_, accumulato2_.numberOfHits as numberO10_0_0_, accumulato2_.prize as prize11_0_0_ from EngineConfigs engineconf0_ left outer join EngineConfigs_Accumulators accumulato1_ on engineconf0_.id=accumulato1_.EngineConfigs_id left outer join Accumulators accumulato2_ on accumulato1_.accumulators_id=accumulato2_.id where engineconf0_.id=?
Hibernate: drop table EngineConfigs if exists

其次是实际错误:

org.springframework.dao.InvalidDataAccessResourceUsageException: could not prepare statement; SQL [select engineconf0_.id as id1_1_1_, engineconf0_.created as created2_1_1_, engineconf0_.customData as customDa3_1_1_, engineconf0_.customerId as customer4_1_1_, engineconf0_.signature as signatur5_1_1_, engineconf0_.signatureVersion as signatur6_1_1_, engineconf0_.updated as updated7_1_1_, engineconf0_.advancedConfiguration as advanced8_1_1_, engineconf0_.engineConfigName as engineCo9_1_1_, engineconf0_.engineServiceName as engineS10_1_1_, engineconf0_.locked as locked11_1_1_, engineconf0_.probabilityMaximum as probabi12_1_1_, accumulato1_.EngineConfigs_id as EngineCo1_1_3_, accumulato2_.id as accumula2_2_3_, accumulato2_.id as id1_0_0_, accumulato2_.created as created2_0_0_, accumulato2_.customData as customDa3_0_0_, accumulato2_.customerId as customer4_0_0_, accumulato2_.signature as signatur5_0_0_, accumulato2_.signatureVersion as signatur6_0_0_, accumulato2_.updated as updated7_0_0_, accumulato2_.accumulatorNumber as accumula8_0_0_, accumulato2_.description as descript9_0_0_, accumulato2_.engineConfig_id as engineC12_0_0_, accumulato2_.numberOfHits as numberO10_0_0_, accumulato2_.prize as prize11_0_0_ from EngineConfigs engineconf0_ left outer join EngineConfigs_Accumulators accumulato1_ on engineconf0_.id=accumulato1_.EngineConfigs_id left outer join Accumulators accumulato2_ on accumulato1_.accumulators_id=accumulato2_.id where engineconf0_.id=?]; nested exception is org.hibernate.exception.SQLGrammarException: could not prepare statement
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.convertHibernateAccessException(HibernateJpaDialect.java:172)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:155)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:417)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:59)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:213)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:147)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.jpa.repository.support.CrudMethodMetadataPostProcessor$CrudMethodMetadataPopulatingMethodIntercceptor.invoke(CrudMethodMetadataPostProcessor.java:122)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:207)
at com.sun.proxy.$Proxy64.save(Unknown Source)
at com.twelve40.gameengine.scenario.ScenarioServiceTest.init(ScenarioServiceTest.java:138)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:24)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:73)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:82)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:73)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:224)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:83)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:68)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:163)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$2.call(DelegatingPowerMockRunner.java:148)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner$2.call(DelegatingPowerMockRunner.java:140)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.withContextClassLoader(DelegatingPowerMockRunner.java:131)
at org.powermock.modules.junit4.internal.impl.DelegatingPowerMockRunner.run(DelegatingPowerMockRunner.java:140)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:121)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
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:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
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.hibernate.exception.SQLGrammarException: could not prepare statement
at org.hibernate.exception.internal.SQLStateConversionDelegate.convert(SQLStateConversionDelegate.java:123)
at org.hibernate.exception.internal.StandardSQLExceptionConverter.convert(StandardSQLExceptionConverter.java:49)
at org.hibernate.engine.jdbc.spi.SqlExceptionHelper.convert(SqlExceptionHelper.java:126)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:196)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl.prepareQueryStatement(StatementPreparerImpl.java:160)
at org.hibernate.loader.Loader.prepareQueryStatement(Loader.java:1885)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1862)
at org.hibernate.loader.Loader.executeQueryStatement(Loader.java:1839)
at org.hibernate.loader.Loader.doQuery(Loader.java:910)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:355)
at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:325)
at org.hibernate.loader.Loader.loadEntity(Loader.java:2149)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:78)
at org.hibernate.loader.entity.AbstractEntityLoader.load(AbstractEntityLoader.java:68)
at org.hibernate.persister.entity.AbstractEntityPersister.load(AbstractEntityPersister.java:4126)
at org.hibernate.event.internal.DefaultLoadEventListener.loadFromDatasource(DefaultLoadEventListener.java:503)
at org.hibernate.event.internal.DefaultLoadEventListener.doLoad(DefaultLoadEventListener.java:468)
at org.hibernate.event.internal.DefaultLoadEventListener.load(DefaultLoadEventListener.java:213)
at org.hibernate.event.internal.DefaultLoadEventListener.proxyOrLoad(DefaultLoadEventListener.java:275)
at org.hibernate.event.internal.DefaultLoadEventListener.onLoad(DefaultLoadEventListener.java:151)
at org.hibernate.internal.SessionImpl.fireLoad(SessionImpl.java:1070)
at org.hibernate.internal.SessionImpl.access$2000(SessionImpl.java:176)
at org.hibernate.internal.SessionImpl$IdentifierLoadAccessImpl.load(SessionImpl.java:2551)
at org.hibernate.internal.SessionImpl.get(SessionImpl.java:960)
at org.hibernate.event.internal.DefaultMergeEventListener.entityIsDetached(DefaultMergeEventListener.java:306)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:186)
at org.hibernate.event.internal.DefaultMergeEventListener.onMerge(DefaultMergeEventListener.java:85)
at org.hibernate.internal.SessionImpl.fireMerge(SessionImpl.java:876)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:858)
at org.hibernate.internal.SessionImpl.merge(SessionImpl.java:863)
at org.hibernate.jpa.spi.AbstractEntityManagerImpl.merge(AbstractEntityManagerImpl.java:1196)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:291)
at com.sun.proxy.$Proxy58.merge(Unknown Source)
at org.springframework.data.jpa.repository.support.SimpleJpaRepository.save(SimpleJpaRepository.java:434)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.executeMethodOn(RepositoryFactorySupport.java:414)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.doInvoke(RepositoryFactorySupport.java:399)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.invoke(RepositoryFactorySupport.java:371)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.data.projection.DefaultMethodInvokingMethodInterceptor.invoke(DefaultMethodInvokingMethodInterceptor.java:61)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:281)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:136)
... 50 more
Caused by: org.h2.jdbc.JdbcSQLException: Table "EngineConfigs" not found; SQL statement:
select engineconf0_.id as id1_1_1_, engineconf0_.created as created2_1_1_, engineconf0_.customData as customDa3_1_1_, engineconf0_.customerId as customer4_1_1_, engineconf0_.signature as signatur5_1_1_, engineconf0_.signatureVersion as signatur6_1_1_, engineconf0_.updated as updated7_1_1_, engineconf0_.advancedConfiguration as advanced8_1_1_, engineconf0_.engineConfigName as engineCo9_1_1_, engineconf0_.engineServiceName as engineS10_1_1_, engineconf0_.locked as locked11_1_1_, engineconf0_.probabilityMaximum as probabi12_1_1_, accumulato1_.EngineConfigs_id as EngineCo1_1_3_, accumulato2_.id as accumula2_2_3_, accumulato2_.id as id1_0_0_, accumulato2_.created as created2_0_0_, accumulato2_.customData as customDa3_0_0_, accumulato2_.customerId as customer4_0_0_, accumulato2_.signature as signatur5_0_0_, accumulato2_.signatureVersion as signatur6_0_0_, accumulato2_.updated as updated7_0_0_, accumulato2_.accumulatorNumber as accumula8_0_0_, accumulato2_.description as descript9_0_0_, accumulato2_.engineConfig_id as engineC12_0_0_, accumulato2_.numberOfHits as numberO10_0_0_, accumulato2_.prize as prize11_0_0_ from EngineConfigs engineconf0_ left outer join EngineConfigs_Accumulators accumulato1_ on engineconf0_.id=accumulato1_.EngineConfigs_id left outer join Accumulators accumulato2_ on accumulato1_.accumulators_id=accumulato2_.id where engineconf0_.id=? [42102-193]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345)
at org.h2.message.DbException.get(DbException.java:179)
at org.h2.message.DbException.get(DbException.java:155)
at org.h2.command.Parser.readTableOrView(Parser.java:5389)
at org.h2.command.Parser.readTableFilter(Parser.java:1257)
at org.h2.command.Parser.parseSelectSimpleFromPart(Parser.java:1897)
at org.h2.command.Parser.parseSelectSimple(Parser.java:2045)
at org.h2.command.Parser.parseSelectSub(Parser.java:1891)
at org.h2.command.Parser.parseSelectUnion(Parser.java:1709)
at org.h2.command.Parser.parseSelect(Parser.java:1697)
at org.h2.command.Parser.parsePrepared(Parser.java:445)
at org.h2.command.Parser.parse(Parser.java:317)
at org.h2.command.Parser.parse(Parser.java:289)
at org.h2.command.Parser.prepareCommand(Parser.java:254)
at org.h2.engine.Session.prepareLocal(Session.java:561)
at org.h2.engine.Session.prepareCommand(Session.java:502)
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1203)
at org.h2.jdbc.JdbcPreparedStatement.<init>(JdbcPreparedStatement.java:73)
at org.h2.jdbc.JdbcConnection.prepareStatement(JdbcConnection.java:287)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$5.doPrepare(StatementPreparerImpl.java:162)
at org.hibernate.engine.jdbc.internal.StatementPreparerImpl$StatementPreparationTemplate.prepareStatement(StatementPreparerImpl.java:186)
... 99 more

如果我从连接URL中删除参数; DB_CLOSE_DELAY = -1并且我从IntelliJ运行测试,则会发生此错误。但是使用参数,测试工作来自IntelliJ,但不是来自maven!我不知道是什么导致这种情况,AFAIK maven不应该影响测试的执行。在执行测试之前,似乎正在关闭与数据库的连接。救命啊!

3 个答案:

答案 0 :(得分:5)

问题在于我正在使用的库。由于我将库项目加载到IntelliJ中,当我手动运行测试时,正在使用库代码。但Maven使用了.m2存储库中的库,该存储库没有最新的.jar。这个过时的jar有一些导致创建表EngineConfigs失败的东西。 如果有人遇到同样的问题,一个有用的技巧是删除&#34;:mem&#34;从数据库连接URL并检查运行测试时生成的* .trace.db文件。该跟踪文件将为您提供有关真正潜在问题的信息。

答案 1 :(得分:3)

FWIW,其他需要查看的是在 Intellij 中用作“项目 SDK”的 Java 版本与您在 CLI/shell 中使用的 Java 版本。对我来说,项目使用 1.8,外壳使用 14。从 Maven 插件运行安装一切正常,但外壳中的一些测试失败。我将 JAVA_HOME 指向 JDK 8,测试全部通过。

答案 2 :(得分:0)

似乎潜在的问题是无法找到表:

Caused by: org.h2.jdbc.JdbcSQLException: Table "EngineConfigs" not found; SQL statement:

也许当您在Maven中运行测试时,您的数据库配置参数(URL等)与您的代码(jdbc:h2:mem:./ScenarioService;DATABASE_TO_UPPER=false;DB_CLOSE_DELAY=-1等)中指定的不同。

我会尝试找出一些application.properties文件(例如位于src\test\resources上)是否覆盖了Maven执行中的这些参数。