環境ごとに異なる構成ファイルが必要になるという典型的な問題があるWebアプリケーションがあります。一部の構成はJNDIデータソースとしてアプリケーションサーバーに配置されますが、一部の構成はプロパティファイルに残ります。
したがって、Springプロファイル機能を使用したいと思います。
私の問題は、テストケースが実行されないことです。
context.xml:
<context:property-placeholder
location="classpath:META-INF/spring/config_${spring.profiles.active}.properties"/>
テスト:
@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
TestPreperationExecutionListener.class
})
@Transactional
@ActiveProfiles(profiles = "localtest")
@ContextConfiguration(locations = {
"classpath:context.xml" })
public class TestContext {
@Test
public void testContext(){
}
}
問題は、プロファイルをロードするための変数が解決されていないことのようです。
Caused by: java.io.FileNotFoundException: class path resource [META-INF/spring/config_${spring.profiles.active}.properties] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:157)
at org.springframework.core.io.support.PropertiesLoaderSupport.loadProperties(PropertiesLoaderSupport.java:181)
at org.springframework.core.io.support.PropertiesLoaderSupport.mergeProperties(PropertiesLoaderSupport.java:161)
at org.springframework.context.support.PropertySourcesPlaceholderConfigurer.postProcessBeanFactory(PropertySourcesPlaceholderConfigurer.java:138)
... 31 more
現在のプロファイルは@ActiveProfile
注釈付きで設定する必要があります。テストケースなので、は使用できませんweb.xml
。可能であれば、ランタイムオプションも避けたいと思います。テストはそのまま実行する必要があります(可能な場合)。
プロファイルを適切にアクティブ化するにはどうすればよいですか?context.xmlでプロファイルを設定することは可能ですか?実際に通常のコンテキストを呼び出しているtest-context.xmlで変数を宣言できますか?