2016-04-14 10 views
1

私は私の春ブーツメインクラスを持っている:オーバーライド@PropertySource

@SpringBootApplication 
@PropertySource("file:/my/file/properties") 
public class Application extends SpringBootServletInitializer { 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { 
     return builder.sources(Application.class); 
    } 
    //main method 
} 

私は(@PropertySourceを使用して)外部ファイルからプロパティを読んでいます。今、私は統合テストがあります。

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes= Application.class) 
@WebIntegrationTest 
@TestPropertySource("file:/my/test/file/properties") // <--- 
public class MyTest { 
    //some tests 
} 

を私はApplicationクラスで@PropertySourceに示されているとは異なり、別の外部プロパティファイルを使用する必要があります。そのため、私は@TestPropertySourceを追加しましたが、この注釈は@PropertySourceを上書きしていないようです。

どうすればよいですか?

ありがとうございます。

答えて

6

このようにそれを使用します。

@TestPropertySource(locations = "classpath:test.properties") 

と場所テストプロパティをJava8でsrc/test/resources

+0

ありがとうございました。できます。 –

0

にファイルを次のような注釈を "リピート" することができます

@PropertySource(value = "file:./src/main/resources/mydefault.properties") 
@PropertySource(value = "file:./src/test/resources/override.properties", ignoreResourceNotFound = true) 

この方法は、後者可能であれば、最初のファイルからプロパティを上書きします。

関連する問題