简体   繁体   English

java.lang.IllegalStateException:使用HSQL嵌入式数据库运行单元测试时,无法加载ApplicationContext

[英]java.lang.IllegalStateException: Failed to load ApplicationContext when running unit test with HSQL embedded database

I am facing a strange situation, where I am able to run my db integration unit test fine, when run standalone, but fails when run with some other tests which use the same autowired DB configuration. 我面临一个奇怪的情况,在独立运行时,我可以很好地运行数据库集成单元测试,但是在与其他使用相同自动连接数据库配置的测试一起运行时,则失败。 Here's the setup I have 这是我的设置

package com.abc;
@Configuration
public class EmbeddedDBTestConfiguration {

    @Bean
    public EmbeddedDBConnectionLocator embeddedDBConnectionLocator() {
        return new EmbeddedDBConnectionLocator();
    }

    @Bean(destroyMethod = "shutdown")
    public DataSource dataSource() {
        return new EmbeddedDatabaseBuilder().addScript(
                "classpath:/xyz.sql")
                .build();
    }

}



package com.abc;  
 public class EmbeddedDBConnectionLocator implements ConnectionLocator {
        @Autowired
        private DataSource embeddedDatabase;

        @Override
        public Connection getConnection() {
            try {
                return embeddedDatabase.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
                return null;
            }
        }
    }

Now, suppose there is a class(in the same package) using the configuration 现在,假设有一个使用该配置的类(在同一包中)

package com.abc;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {EmbeddedDBTestConfiguration.class})
public class AbcIntegrationTest {
}

There is another class in another package using the same EmbeddedDBTestConfiguration 使用相同的EmbeddedDBTestConfiguration的另一个程序包中还有另一个类

package com.xyz;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {EmbeddedDBTestConfiguration.class})
public class XyzIntegrationTest {
   public void testA(){ 
     // do some testing with the DB
   }

   public void testB(){
     // do some testing with the DB
   }
}

When I run a mvn goal for all integration tests, XyzIntegrationTest results in the following error 当我为所有集成测试运行mvn目标时,XyzIntegrationTest导致以下错误

testA(com.xyz.XyzIntegrationTest) Time elapsed: ... ERROR!
java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.error.Error.error(Unknown Source)
    at org.hsqldb.SchemaManager.dropSchema(Unknown Source)
    at org.hsqldb.StatementSchema.dropSchema(Unknown Source)
    at org.hsqldb.StatementSchema.getResult(Unknown Source)
    at org.hsqldb.StatementSchema.execute(Unknown Source)
    at org.hsqldb.Session.executeCompiledStatement(Unknown Source)
    at org.hsqldb.Session.executeDirectStatement(Unknown Source)
    at org.hsqldb.Session.execute(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.fetchResult(Unknown Source)
    at org.hsqldb.jdbc.JDBCStatement.execute(Unknown Source)
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.executeSqlScript(ResourceDatabasePopulator.java:186)
    at org.springframework.jdbc.datasource.init.ResourceDatabasePopulator.populate(ResourceDatabasePopulator.java:134)
    at org.springframework.jdbc.datasource.init.DatabasePopulatorUtils.execute(DatabasePopulatorUtils.java:47)
    at org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory.initDatabase(EmbeddedDatabaseFactory.java:159)
    at org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory.getDatabase(EmbeddedDatabaseFactory.java:132)
    at org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseBuilder.build(EmbeddedDatabaseBuilder.java:116)
    at com.cobaltgroup.websites.websiteswebapp.itest.shiftdigital.lead.EmbeddedDBTestConfiguration.dataSource(EmbeddedDBTestConfiguration.java:19)
    at com.cobaltgroup.websites.websiteswebapp.itest.shiftdigital.lead.EmbeddedDBTestConfiguration$$EnhancerBySpringCGLIB$$fd06a2f1.CGLIB$dataSource$1(<generated>)
    at com.cobaltgroup.websites.websiteswebapp.itest.shiftdigital.lead.EmbeddedDBTestConfiguration$$EnhancerBySpringCGLIB$$fd06a2f1$$FastClassBySpringCGLIB$$8667dac.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)
    at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:292)
    at com.cobaltgroup.websites.websiteswebapp.itest.shiftdigital.lead.EmbeddedDBTestConfiguration$$EnhancerBySpringCGLIB$$fd06a2f1.dataSource(<generated>)
    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.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:160)
    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:592)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1057)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:953)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:487)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:921)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:864)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:503)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:295)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1148)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:921)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:864)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:779)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:445)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:423)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:551)
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:169)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:304)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1148)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:293)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:290)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:191)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:636)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:934)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:120)
    at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:102)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:246)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContextInternal(CacheAwareContextLoaderDelegate.java:69)
    at org.springframework.test.context.CacheAwareContextLoaderDelegate.loadContext(CacheAwareContextLoaderDelegate.java:95)
    at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:122)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:109)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:75)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:321)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:211)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:288)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:284)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:231)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:88)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
    at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
    at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:174)
    at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:264)
    at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
    at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:124)
    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.invokeMethodWithArray2(ReflectionUtils.java:208)
    at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:159)
    at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:87)
    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:95)

testB(com.xyz.XyzIntegrationTest)   Time elapsed: 1.79 sec

So, testA() fails, but testB and the rest of the other tests within the class runs fine. 因此,testA()失败,但是该类中的testB和其他测试仍然可以正常运行。 Also, If I "@Ignore" AbcIntegrationTest, this test and other tests within the "xyz" package(using the DB configuration) run fine. 另外,如果我“ @Ignore” AbcIntegrationTest,则此测试和“ xyz”包中的其他测试(使用数据库配置)运行良好。 Can anyone please tell me why this might be happening? 谁能告诉我为什么会这样吗? Thank you! 谢谢!

Okay, I think I got the solution. 好的,我想我已经解决了。 I just had to use a @DirtiesContext and that fixed it for me, so marking this question as answered...! 我只需要使用@DirtiesContext并为我修复它,因此将此问题标记为已回答...!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 测试失败:java.lang.IllegalStateException:加载ApplicationContext失败 - Test Failure : java.lang.IllegalStateException: Failed to load ApplicationContext 测试 java.lang.IllegalStateException: 加载 ApplicationContext 失败 - Test java.lang.IllegalStateException: Failed to load ApplicationContext java.lang.IllegalStateException:无法加载ApplicationContext - java.lang.IllegalStateException: Failed to load ApplicationContext 运行SpringWS JUnit Test时发生异常:java.lang.IllegalStateException:无法加载ApplicationContext - Exception While Running SpringWS JUnit Test : java.lang.IllegalStateException: Failed to load ApplicationContext 在Spring Web App中运行junit时,出现错误:java.lang.IllegalStateException:无法加载ApplicationContext - when running junit in spring web app, get an error: java.lang.IllegalStateException: Failed to load ApplicationContext 我正在尝试在 springboot 中进行单元测试,但测试抛出 java.lang.IllegalStateException: Failed to load ApplicationContext - I´m trying to make a unit test in springboot but the test throw me java.lang.IllegalStateException: Failed to load ApplicationContext 使用Spring编写数据库单元测试:java.lang.IllegalStateException:无法加载ApplicationContext - Writing DB Unit Tests with Spring : java.lang.IllegalStateException: Failed to load ApplicationContext Junit错误:java.lang.IllegalStateException:无法加载ApplicationContext - Junit error :java.lang.IllegalStateException: Failed to load ApplicationContext Spring JUnit-java.lang.IllegalStateException:无法加载ApplicationContext - Spring JUnit - java.lang.IllegalStateException: Failed to load ApplicationContext SpringBoot + Mybatis + MySQL,java.lang.IllegalStateException:无法加载ApplicationContext - SpringBoot + Mybatis + MySQL, java.lang.IllegalStateException: Failed to load ApplicationContext
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM