Spring 在集成测试期间引导未解析来自 application.properties 的自定义属性
Spring boot not resolving custom properties from application.properties during integration tests
我的Spring开机测试配置如下:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {TestEnd2EndIntegrationConfiguration.class})
@WebAppConfiguration
@ActiveProfiles(Profiles.TEST)//=="test"
@Transactional
public abstract class AbstractEnd2EndIntegrationTest {
}
然后从 application-test.properties
我有以下 custom 属性:
database.hibernate.dialect=org.hibernate.dialect.H2Dialect
database.hibernate.hbm2ddl.auto=create
database.hibernate.logSqlInfo=true
我使用如下:
@Value("${database.hibernate.dialect}")
private String hibernateDialect;
但是,自定义属性没有得到解决,我得到了如下异常:
com.bignibou.it.controller.signup.SignupRestControllerTest > shouldRejectUnavailableEmailAddress FAILED
java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException
编辑:
这是我的 gradle 配置:
sourceSets {
main {
output.resourcesDir = output.classesDir
}
integrationTest {
java.srcDirs = ['src/it/java']
resources.srcDirs = ['src/it/resources', 'src/main/resources']
compileClasspath = sourceSets.main.output + configurations.testRuntime
runtimeClasspath = output + compileClasspath
}
}
task integrationTest(type: Test) {
description "Run the integration tests."
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
reports.html.destination = file("$reports.html.destination/integration")
reports.junitXml.destination = file("$reports.junitXml.destination/integration")
}
编辑 2:
当我 运行 来自 IDE (intellij) 的测试时,我得到一个不同的错误:
ava:342)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:273)
at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:103)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 36 more
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.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:183)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:123)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:217)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:276)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:278)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=17=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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:180)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=17=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassCastException: org.springframework.boot.context.config.ConfigFileApplicationListener$ConfigurationPropertySources cannot be cast to org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor$ConfigurationPropertySources
at org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor$ConfigurationPropertySources.finishAndRelocate(ConfigFileEnvironmentPostProcessor.java:528)
at org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor$PropertySourceOrderingPostProcessor.reorderSources(ConfigFileEnvironmentPostProcessor.java:239)
at org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor$PropertySourceOrderingPostProcessor.postProcessBeanFactory(ConfigFileEnvironmentPostProcessor.java:235)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:131)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:673)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:519)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:667)
at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:342)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:273)
at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:103)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 36 more
编辑 3:
这是来自 Gradle 集成测试
的详细原因
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [${database.hibernate.ejb.naming_strategy}] as strategy [org.hibernate.cfg.NamingStrategy]
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:128)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:155)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:136)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.processProperties(EntityManagerFactoryBuilderImpl.java:925)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:840)
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
编辑 4:来自以下调试:EntityManagerFactoryBuilderImpl
:
configurationValues = {HashMap@4101} size = 14
0 = {HashMap$Node@4092} "hibernate.format_sql" -> "${database.hibernate.logSqlInfo}"
1 = {HashMap$Node@4096} "hibernate.use_sql_comments" -> "${database.hibernate.logSqlInfo}"
2 = {HashMap$Node@4107} "javax.persistence.nonJtaDataSource" ->
3 = {HashMap$Node@4108} "hibernate.hbm2ddl.auto" -> "${database.hibernate.hbm2ddl.auto}"
4 = {HashMap$Node@4109} "javax.persistence.sharedCache.mode" -> "ENABLE_SELECTIVE"
5 = {HashMap$Node@4110} "hibernate.dialect" -> "${database.hibernate.dialect}"
6 = {HashMap$Node@4111} "hibernate.ejb.naming_strategy" -> "${database.hibernate.ejb.naming_strategy}"
7 = {HashMap$Node@4112} "hibernate.cache.use_second_level_cache" -> "${database.hibernate.cache.use_second_level_cache}"
8 = {HashMap$Node@4113} "hibernate.ejb.persistenceUnitName" -> "default"
9 = {HashMap$Node@4114} "hibernate.connection.charSet" -> "${database.hibernate.connection.charSet}"
10 = {HashMap$Node@4115} "hibernate.show_sql" -> "${database.hibernate.logSqlInfo}"
11 = {HashMap$Node@4116} "hibernate.cache.region.factory_class" -> "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"
12 = {HashMap$Node@4117} "hibernate.generate_statistics" -> "${database.hibernate.generate_statistics}"
13 = {HashMap$Node@4118} "javax.persistence.validation.mode" -> "AUTO"
可以看出属性没有展开...这是为什么呢?
您需要 3 件事才能让您的属性在集成测试中自动装配 classes:
PropertySource
注释。看起来你错过了那个。
您可以将类似的内容添加到您的抽象集成测试 class:
@PropertySource(value= {"classpath:*.properties"})
- 属性 文件必须在您的 class 路径中。我建议为测试和集成测试属性添加一个单独的文件夹。在
src/main
中创建一个 integration-test-resources
目录并将您的 属性 文件复制到那里,然后配置您的构建管理器以将它们添加为 class 路径资源。
下面是如何在 Gradle 中执行此操作的示例:
sourceSets {
integrationTest {
resources {
srcDir "integration-test-resources"
}
}
}
- 用
@Value
注释你的 属性。看起来你已经在这样做了。
如果您想将 Hibernate 属性放入 application-${profile}.properties
,即 Spring 属性文件,我认为您应该使用 spring.jpa.hibernate.dll-auto
。
同样,应该使用spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
。
我使用 Spring 这样的配置启动,我从来不需要任何 @PropertySource
注释。
问题实际上出在以下代码行:
@SpringApplicationConfiguration(classes = {TestEnd2EndIntegrationConfiguration.class})
TestEnd2EndIntegrationConfiguration
不是 Spring 带有主程序的引导应用程序,最重要的是它没有使用 @EnableAutoConfiguration
注释进行注释。
我改为:
@Configuration
@EnableAutoConfiguration(exclude = {
ManagementWebSecurityAutoConfiguration.class,
...
MailSenderAutoConfiguration.class
})
@ComponentScan(basePackages = {"com.bignibou.configuration", "com.bignibou.it.configuration"})
public class TestApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(TestApplication.class, args);
}
}
和我的抽象测试class:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {TestApplication.class})//Notice the change
@WebAppConfiguration
@ActiveProfiles(Profiles.TEST)
@Transactional
public abstract class AbstractIntegrationTest {
}
现在 运行 作为 spring 启动应用程序的测试和 application.properties
中的自定义属性也已解决。
我的Spring开机测试配置如下:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {TestEnd2EndIntegrationConfiguration.class})
@WebAppConfiguration
@ActiveProfiles(Profiles.TEST)//=="test"
@Transactional
public abstract class AbstractEnd2EndIntegrationTest {
}
然后从 application-test.properties
我有以下 custom 属性:
database.hibernate.dialect=org.hibernate.dialect.H2Dialect
database.hibernate.hbm2ddl.auto=create
database.hibernate.logSqlInfo=true
我使用如下:
@Value("${database.hibernate.dialect}")
private String hibernateDialect;
但是,自定义属性没有得到解决,我得到了如下异常:
com.bignibou.it.controller.signup.SignupRestControllerTest > shouldRejectUnavailableEmailAddress FAILED
java.lang.IllegalStateException
Caused by: org.springframework.beans.factory.BeanCreationException
Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException
编辑:
这是我的 gradle 配置:
sourceSets {
main {
output.resourcesDir = output.classesDir
}
integrationTest {
java.srcDirs = ['src/it/java']
resources.srcDirs = ['src/it/resources', 'src/main/resources']
compileClasspath = sourceSets.main.output + configurations.testRuntime
runtimeClasspath = output + compileClasspath
}
}
task integrationTest(type: Test) {
description "Run the integration tests."
testClassesDir = sourceSets.integrationTest.output.classesDir
classpath = sourceSets.integrationTest.runtimeClasspath
reports.html.destination = file("$reports.html.destination/integration")
reports.junitXml.destination = file("$reports.junitXml.destination/integration")
}
编辑 2:
当我 运行 来自 IDE (intellij) 的测试时,我得到一个不同的错误:
ava:342)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:273)
at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:103)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 36 more
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.web.ServletTestExecutionListener.setUpRequestContextIfNecessary(ServletTestExecutionListener.java:183)
at org.springframework.test.context.web.ServletTestExecutionListener.prepareTestInstance(ServletTestExecutionListener.java:123)
at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:228)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:217)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runReflectiveCall(SpringJUnit4ClassRunner.java:276)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:278)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:236)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=17=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.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:180)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access[=17=]0(ParentRunner.java:58)
at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:78)
at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:212)
at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:68)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Caused by: java.lang.ClassCastException: org.springframework.boot.context.config.ConfigFileApplicationListener$ConfigurationPropertySources cannot be cast to org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor$ConfigurationPropertySources
at org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor$ConfigurationPropertySources.finishAndRelocate(ConfigFileEnvironmentPostProcessor.java:528)
at org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor$PropertySourceOrderingPostProcessor.reorderSources(ConfigFileEnvironmentPostProcessor.java:239)
at org.springframework.boot.context.config.ConfigFileEnvironmentPostProcessor$PropertySourceOrderingPostProcessor.postProcessBeanFactory(ConfigFileEnvironmentPostProcessor.java:235)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:284)
at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:131)
at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:673)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:519)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:667)
at org.springframework.boot.SpringApplication.doRun(SpringApplication.java:342)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:273)
at org.springframework.boot.test.SpringApplicationContextLoader.loadContext(SpringApplicationContextLoader.java:103)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:98)
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:116)
... 36 more
编辑 3:
这是来自 Gradle 集成测试
的详细原因Caused by: org.hibernate.boot.registry.selector.spi.StrategySelectionException: Unable to resolve name [${database.hibernate.ejb.naming_strategy}] as strategy [org.hibernate.cfg.NamingStrategy]
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.selectStrategyImplementor(StrategySelectorImpl.java:128)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveDefaultableStrategy(StrategySelectorImpl.java:155)
at org.hibernate.boot.registry.selector.internal.StrategySelectorImpl.resolveStrategy(StrategySelectorImpl.java:136)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.processProperties(EntityManagerFactoryBuilderImpl.java:925)
at org.hibernate.jpa.boot.internal.EntityManagerFactoryBuilderImpl.build(EntityManagerFactoryBuilderImpl.java:840)
at org.hibernate.jpa.HibernatePersistenceProvider.createContainerEntityManagerFactory(HibernatePersistenceProvider.java:152)
at org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean.createNativeEntityManagerFactory(LocalContainerEntityManagerFactoryBean.java:343)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.afterPropertiesSet(AbstractEntityManagerFactoryBean.java:318)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1637)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1574)
编辑 4:来自以下调试:EntityManagerFactoryBuilderImpl
:
configurationValues = {HashMap@4101} size = 14
0 = {HashMap$Node@4092} "hibernate.format_sql" -> "${database.hibernate.logSqlInfo}"
1 = {HashMap$Node@4096} "hibernate.use_sql_comments" -> "${database.hibernate.logSqlInfo}"
2 = {HashMap$Node@4107} "javax.persistence.nonJtaDataSource" ->
3 = {HashMap$Node@4108} "hibernate.hbm2ddl.auto" -> "${database.hibernate.hbm2ddl.auto}"
4 = {HashMap$Node@4109} "javax.persistence.sharedCache.mode" -> "ENABLE_SELECTIVE"
5 = {HashMap$Node@4110} "hibernate.dialect" -> "${database.hibernate.dialect}"
6 = {HashMap$Node@4111} "hibernate.ejb.naming_strategy" -> "${database.hibernate.ejb.naming_strategy}"
7 = {HashMap$Node@4112} "hibernate.cache.use_second_level_cache" -> "${database.hibernate.cache.use_second_level_cache}"
8 = {HashMap$Node@4113} "hibernate.ejb.persistenceUnitName" -> "default"
9 = {HashMap$Node@4114} "hibernate.connection.charSet" -> "${database.hibernate.connection.charSet}"
10 = {HashMap$Node@4115} "hibernate.show_sql" -> "${database.hibernate.logSqlInfo}"
11 = {HashMap$Node@4116} "hibernate.cache.region.factory_class" -> "org.hibernate.cache.ehcache.SingletonEhCacheRegionFactory"
12 = {HashMap$Node@4117} "hibernate.generate_statistics" -> "${database.hibernate.generate_statistics}"
13 = {HashMap$Node@4118} "javax.persistence.validation.mode" -> "AUTO"
可以看出属性没有展开...这是为什么呢?
您需要 3 件事才能让您的属性在集成测试中自动装配 classes:
PropertySource
注释。看起来你错过了那个。 您可以将类似的内容添加到您的抽象集成测试 class:
@PropertySource(value= {"classpath:*.properties"})
- 属性 文件必须在您的 class 路径中。我建议为测试和集成测试属性添加一个单独的文件夹。在
src/main
中创建一个integration-test-resources
目录并将您的 属性 文件复制到那里,然后配置您的构建管理器以将它们添加为 class 路径资源。
下面是如何在 Gradle 中执行此操作的示例:
sourceSets {
integrationTest {
resources {
srcDir "integration-test-resources"
}
}
}
- 用
@Value
注释你的 属性。看起来你已经在这样做了。
如果您想将 Hibernate 属性放入 application-${profile}.properties
,即 Spring 属性文件,我认为您应该使用 spring.jpa.hibernate.dll-auto
。
同样,应该使用spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
。
我使用 Spring 这样的配置启动,我从来不需要任何 @PropertySource
注释。
问题实际上出在以下代码行:
@SpringApplicationConfiguration(classes = {TestEnd2EndIntegrationConfiguration.class})
TestEnd2EndIntegrationConfiguration
不是 Spring 带有主程序的引导应用程序,最重要的是它没有使用 @EnableAutoConfiguration
注释进行注释。
我改为:
@Configuration
@EnableAutoConfiguration(exclude = {
ManagementWebSecurityAutoConfiguration.class,
...
MailSenderAutoConfiguration.class
})
@ComponentScan(basePackages = {"com.bignibou.configuration", "com.bignibou.it.configuration"})
public class TestApplication {
public static void main(String[] args) throws Exception {
SpringApplication.run(TestApplication.class, args);
}
}
和我的抽象测试class:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = {TestApplication.class})//Notice the change
@WebAppConfiguration
@ActiveProfiles(Profiles.TEST)
@Transactional
public abstract class AbstractIntegrationTest {
}
现在 运行 作为 spring 启动应用程序的测试和 application.properties
中的自定义属性也已解决。