SpringBootTest

时间:2017-11-17 18:26:12

标签: spring hibernate spring-boot

尝试使用@SpringBootTest注释运行单元测试,我从hibernate获得此错误。我在网上找到的所有信息都说要设置" hibernate.id.new_generator_mappings"为假,但这不能解决我的问题。

@RunWith(SpringRunner.class)
@SpringBootTest(classes = AppserverApp.class)
public class DummyTest {

    @Test
    public void dummyTest() {
        fail();
    }
}
  

2017-11-17 13:20:28.885 WARN 11396 --- [主要]   o.s.w.c.s.GenericWebApplicationContext:遇到异常   在上下文初始化期间 - 取消刷新尝试:   org.springframework.beans.factory.BeanCreationException:错误   创建名为&entityManagerFactory'的bean在类路径中定义   资源   [组织/ springframework的/引导/自动配置/ ORM / JPA / HibernateJpaAutoConfiguration.class]:   调用init方法失败;嵌套异常是   javax.persistence.PersistenceException:[PersistenceUnit:default]   无法构建Hibernate SessionFactory 2017-11-17 13:20:28.925 WARN   11396 --- [main] o.s.boot.SpringApplication:   错误处理失败(使用名称创建bean时出错   ' delegatingApplicationListener'在类路径资源中定义   [组织/ springframework的/安全/配置/注解/网络/配置/ WebSecurityConfiguration.class]:   实例化bean之前的BeanPostProcessor失败;嵌套   异常是org.springframework.beans.factory.BeanCreationException:   使用名称创建bean时出错   ' org.springframework.cache.annotation.ProxyCachingConfiguration&#39 ;:   bean的初始化失败;嵌套异常是   org.springframework.beans.factory.NoSuchBeanDefinitionException:没有   豆命名   ' org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry'   2017-11-17 13:20:28.951 ERROR 11396 --- [主要]   o.s.boot.SpringApplication:应用程序启动失败

     

org.springframework.beans.factory.BeanCreationException:错误   创建名为&entityManagerFactory'的bean在类路径中定义   资源   [组织/ springframework的/引导/自动配置/ ORM / JPA / HibernateJpaAutoConfiguration.class]:   调用init方法失败;嵌套异常是   javax.persistence.PersistenceException:[PersistenceUnit:default]   无法构建Hibernate SessionFactory   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1628)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:483)     在   org.springframework.beans.factory.support.AbstractBeanFactory $ 1.getObject(AbstractBeanFactory.java:306)     在   org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)     在   org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)     在   org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)     在   org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1081)     在   org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:856)     在   org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:542)     在   org.springframework.boot.SpringApplication.refresh(SpringApplication.java:737)     在   org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:370)     在   org.springframework.boot.SpringApplication.run(SpringApplication.java:314)     在   org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:120)     在   org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)     在   org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)     在   org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)     在   org.springframework.test.context.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:189)     在   org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:131)     在   org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:230)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:228)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner $ 1.runReflectiveCall(SpringJUnit4ClassRunner.java:287)     在   org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:289)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:247)     在   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:94)     在org.junit.runners.ParentRunner $ 3.run(ParentRunner.java:290)at at   org.junit.runners.ParentRunner $ 1.schedule(ParentRunner.java:71)at at   org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)at at   org.junit.runners.ParentRunner.access $ 000(ParentRunner.java:58)at at   org.junit.runners.ParentRunner $ 2.evaluate(ParentRunner.java:268)at at   org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)     在   org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)     在org.junit.runners.ParentRunner.run(ParentRunner.java:363)at   org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:191)     在org.junit.runner.JUnitCore.run(JUnitCore.java:137)at   com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:68)     在   com.intellij.rt.execution.junit.IdeaTestRunner $ Repeater.startRunnerWithArgs(IdeaTestRunner.java:51)     在   com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:242)     在   com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:70)   引起:javax.persistence.PersistenceException:[PersistenceUnit:   默认]无法构建Hibernate SessionFactory   org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.persistenceException(EntityManagerFactoryBuilderImpl.java:967)     在   org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:892)     在   org.springframework.orm.jpa.vendor.SpringHibernateJpaPersistenceProvider.createContainerEntityManagerFactory(SpringHibernateJpaPersistenceProvider.java:60)     在   org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:353)     在   org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.buildNativeEntityManagerFactory(AbstractEntityManagerFactoryBean.java:370)     在   org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:359)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1687)     在   org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1624)     ...省略了39个常见帧引起的:   org.hibernate.tool.schema.spi.SchemaManagementException:   架构验证:缺少序列[hibernate_sequence] at   org.hibernate.tool.schema.internal.AbstractSchemaValidator.validateSequence(AbstractSchemaValidator.java:184)     在   org.hibernate.tool.schema.internal.AbstractSchemaValidator.performValidation(AbstractSchemaValidator.java:100)     在   org.hibernate.tool.schema.internal.AbstractSchemaValidator.doValidation(AbstractSchemaValidator.java:68)     在   org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.performDatabaseAction(SchemaManagementToolCoordinator.java:191)     在   org.hibernate.tool.schema.spi.SchemaManagementToolCoordinator.process(SchemaManagementToolCoordinator.java:72)     在   org.hibernate.internal.SessionFactoryImpl。(SessionFactoryImpl.java:309)     在   org.hibernate.boot.internal.SessionFactoryBuilderImpl.build(SessionFactoryBuilderImpl.java:445)     在   org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:889)     ...省略了45个常用帧

2 个答案:

答案 0 :(得分:1)

我使用MySQL作为主要数据库,使用H2作为测试数据库。我的主应用程序运行良好,并且由于我使用的是MySQL,并且我的实体id列已注释为使用身份策略@GeneratedValue(strategy = GenerationType.IDENTITY),因此该异常无关紧要。

因此,比较开发人员和测试的 application.yml 可以向我展示解决方案!

在我的情况下,这是因为在测试配置文件(application.yml)中启用了休眠验证:

spring:
    jpa:
        properties:
            hibernate.hbm2ddl.auto: validate

在我的测试配置文件中删除此属性即可解决此问题。

答案 1 :(得分:0)

我猜您在测试配置文件中启用了hibernate验证,但在运行配置文件中没有启用。