使用Spring BOOT集成测试JPA存储库时的NoSuchMethodError(getMetaModel)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Spring BOOT集成测试JPA存储库时的NoSuchMethodError(getMetaModel)相关的知识,希望对你有一定的参考价值。

我有一个基于Spring BOOT的Web服务,它使用Spring数据JPA连接到mysql DB。我能够毫无问题地运行和部署应用程序。

但是当我运行Integration测试用例时,随着应用程序的配置,我面临一个异常(java.lang.NoSuchMethodError:javax.persistence.EntityManagerFactory.getMetamodel()Ljavax / persistence / metamodel / Metamodel)

这将被触发,因为即将创建扩展JpaRepository接口的存储库bean。

当我检查EntityManagerFactory接口方法时,我找到了getMetaModel()方法。

版本

  • 春天:4.0
  • org.eclipse.persistence.jpa:2.6.3
  • spring-data-jpa:1.9.2.RELEASE

如果任何人都可以指出根本原因或进一步的调试步骤,将会很有帮助。

++++++++++++++++++++++++++++++++++++++++++

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {ApplicationService.class, ApplicationServiceImpl.class})
@EnableTransactionManagement
@WebAppConfiguration public class IntegrationTestBase {
    @Autowired
    private ApplicationServiceImpl service;
..
}

例外

java.lang.IllegalStateException: Failed to load ApplicationContext
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:124)
    at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
    at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
    at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
    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:70)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:119)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:42)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ApplicationServiceImpl': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private de.adclear.soa.integration.repository.ApplicationRepository de.adclear.soa.integration.ApplicationServiceImpl.ApplicationRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ApplicationRepository': Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.EntityManagerFactory.getMetamodel()Ljavax/persistence/metamodel/Metamodel;
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
    at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:133)
    at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:60)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
    at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
    at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
    ... 24 more
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private de.adclear.soa.integration.repository.ApplicationRepository de.adclear.soa.integration.ApplicationServiceImpl.ApplicationRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ApplicationRepository': Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.EntityManagerFactory.getMetamodel()Ljavax/persistence/metamodel/Metamodel;
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:573)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 40 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'ApplicationRepository': Cannot resolve reference to bean 'jpaMappingContext' while setting bean property 'mappingContext'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.EntityManagerFactory.getMetamodel()Ljavax/persistence/metamodel/Metamodel;
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:359)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
    ... 42 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaMappingContext': Invocation of init method failed; nested exception is java.lang.NoSuchMethodError: javax.persistence.EntityManagerFactory.getMetamodel()Ljavax/persistence/metamodel/Metamodel;
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1578)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
    at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
    ... 55 more
Caused by: java.lang.NoSuchMethodError: javax.persistence.EntityManagerFactory.getMetamodel()Ljavax/persistence/metamodel/Metamodel;
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.getMetamodels(JpaMetamodelMappingContextFactoryBean.java:90)
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:56)
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:26)
at org.springframework.beans.factory.config.AbstractFactoryBean.afterPropertiesSet(AbstractFactoryBean.java:134)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:545)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:351)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:108)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1481)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1192)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1116)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1014)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:545)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:133)
at org.springframework.test.context.web.AbstractGenericWebContextLoader.loadContext(AbstractGenericWebContextLoader.java:60)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.delegateLoading(AbstractDelegatingSmartContextLoader.java:109)
at org.springframework.test.context.support.AbstractDelegatingSmartContextLoader.loadContext(AbstractDelegatingSmartContextLoader.java:261)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:83)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:249)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:89)
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:70)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:193)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:283)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:173)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:153)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:128)
at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:203)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:155)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)

执行单元测试时,我记录Spring配置和初始化的步骤,在这些步骤之后抛出异常 -

2016-06-06 11:35:51,840 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(ApplicationServiceConfig)
2016-06-06 11:35:58,372 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(dataSource)
2016-06-06 11:35:58,490 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$DataSourceInitializerConfiguration)
2016-06-06 11:36:00,890 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(spring.datasource.CONFIGURATION_PROPERTIES)
2016-06-06 11:36:00,941 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(dataSourceInitializer)
2016-06-06 11:36:04,189 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(jpaVendorAdapter)
2016-06-06 11:36:05,630 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(jpaDialect)
2016-06-06 11:36:09,985 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(eclipseLinkProperty)
2016-06-06 11:36:16,705 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(entityManagerFactory)
2016-06-06 11:36:17,817 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(org.springframework.context.event.internalEventListenerProcessor)
2016-06-06 11:36:17,820 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(org.springframework.context.event.internalEventListenerFactory)
2016-06-06 11:36:17,827 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(ApplicationServiceApplication)
2016-06-06 11:36:18,160 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean((inner bean)#298a0c11)
2016-06-06 11:36:18,168 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean((inner bean)#29c496d3)
2016-06-06 11:36:18,175 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean((inner bean)#592e883f)
2016-06-06 11:36:18,201 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean((inner bean)#6f672a3f)
2016-06-06 11:36:18,211 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(jpaMappingContext)

当我运行应用程序(没有测试)时,我没有遇到任何异常,以下是记录的步骤 -

2016-06-06 11:49:37,146 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(dataSource)
2016-06-06 11:49:37,152 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration$DataSourceInitializerConfiguration)
2016-06-06 11:49:37,190 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(spring.datasource.CONFIGURATION_PROPERTIES)
2016-06-06 11:49:37,196 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(dataSourceInitializer)
2016-06-06 11:49:37,211 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(jpaVendorAdapter)
2016-06-06 11:49:37,215 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(jpaDialect)
2016-06-06 11:49:37,223 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(eclipseLinkProperty)
2016-06-06 11:49:37,235 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(entityManagerFactory)
2016-06-06 11:49:38,148 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(org.springframework.context.event.internalEventListenerProcessor)
2016-06-06 11:49:38,151 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(org.springframework.context.event.internalEventListenerFactory)
2016-06-06 11:49:38,155 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(ApplicationServiceApplication)
2016-06-06 11:49:38,160 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(swaggerConfig)
2016-06-06 11:49:38,431 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean((inner bean)#3b5eab14)
2016-06-06 11:49:38,436 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean((inner bean)#1bcbbc70)
2016-06-06 11:49:38,441 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean((inner bean)#2f7fd8d2)
2016-06-06 11:49:38,471 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean((inner bean)#7d7e1cb4)
2016-06-06 11:49:38,480 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(jpaMappingContext)
2016-06-06 11:49:38,534 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(ApplicationRepository)
2016-06-06 11:49:39,116 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(ApplicationServiceImpl)
2016-06-06 11:49:39,128 INFO  DebugSpringConfig:18 - postProcessBeforeInitialization: bean(referrerMappingController)
答案

关于进一步调查下面的堆栈跟踪的具体部分:

Caused by: java.lang.NoSuchMethodError: javax.persistence.EntityManagerFactory.getMetamodel()Ljavax/persistence/metamodel/Metamodel;
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.getMetamodels(JpaMetamodelMappingContextFactoryBean.java:90)
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:56)
at org.springframework.data.jpa.repository.config.JpaMetamodelMappingContextFactoryBean.createInstance(JpaMetamodelMappingContextFactoryBean.java:26)

我观察到JpaMetamodelMappingContextFactoryBean.java指的是错误引用的旧版javax.persistence库。

我从maven中取出了上面的lib,并将这个lib的依赖项与org.eclipse.persistence库对齐。这有一个EntityManagerFactory的实现,带有getMetamodel()方法的实现。

这样问题就解决了。

另一答案

我有两个不同版本(1和2)的javax.persistence库,它们是从依赖jar导入的(EclipseLink:2.5.2,包括(javax.persistence:2.1.0)和persistence-api:1.0.2。我只需要要做的是将EclipseLink移到pom.xml中具有persistence-api:1.0.2的项目之前。依赖顺序在document中定义:因为Maven 2.0.9它是声明中的顺序:第一次宣言获胜。

以上是关于使用Spring BOOT集成测试JPA存储库时的NoSuchMethodError(getMetaModel)的主要内容,如果未能解决你的问题,请参考以下文章

从其他实体检索ID时的Spring Boot JPA反序列化问题

在 Spring Boot 应用程序的 JUnit 测试中,自动装配的 JPA 存储库没有合格的 bean

spring-boot与spring-data-JPA的简单集成使用

集成 Spring JPA Data 和 Spring Cache 时的奇怪行为

spring boot 1.5.4 集成spring-Data-JPA

JPA Spring Boot 微服务 - 使用两个多对一映射持久化实体时的无限循环