2017-03-21 10 views
0

integrationTest設定でgradle(3.4.1)を使用すると、アプリケーションが正しく初期化されてもSpringブート(1.5.1.RELEASE)のConfigurationPropertiesを使用したテストが失敗します./gradlew bootRun)。 ConfigurationPropertiesで注釈が付けられたクラスは、次のようになります。統合テスト用にSpringブート設定プロパティを初期化できません

@Component 
@ConfigurationProperties(prefix = "foo") 
@Validated 
public class AppConfiguration { 
    @NonNull 
    private URL serviceUrl; 
    ... 

設定ファイルには、ゲッターとセッターがあります。次のように生成されたエラーは、統合テストの構成クラスが注釈され

java.lang.IllegalStateException: Failed to load ApplicationContext 
.... 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'AppConfiguration': Could not bind properties to AppConfiguration 
.... 
Caused by: org.springframework.validation.BindException: org.springframework.boot.bind.RelaxedDataBinder$RelaxedBeanPropertyBindingResult 
Field error in object 'foo' on field 'serviceUrl': rejected value [null]; codes ... 

次のような

@Configuration 
@ComponentScan(...) 
@EnableConfigurationProperties 
@EnableIntegration 
public static class ContextConfiguration {} 

答えて

0

テストクラスであった以下の注釈

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration 
public class ReleaseTest { 
... 

後ConfigurationPropertiesBindingPostProcessor#postProcessBeforeInitialization()のSpringブートコードを見てみると、プロパティーソースがないと示唆されました発見されたg。 AppConfigurationクラスはYAMLを用いて適切に初期化されたコンパイル時の依存関係としてばねブートスタータ試験アーチファクトと

@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class) 

にテストクラスのコンテキストコンフィギュレーションを変更する:org.springframework.bootを添加ベースのプロパティファイル。

代替はしません。このアプローチは春・ブート・スターターテスト依存性を必要としない

@TestPropertySource("classpath:/application.properties") 

を追加することで、「伝統的な」プロパティファイル(YAMLファイルを使用されている必要がありますこのアプローチで作業してください)。

関連する問題