2016-04-14 74 views
1

我有我的春天啓動的主類:與@TestPropertySource覆蓋@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") 

和地點測試屬性文件到src/test/resources

+0

謝謝。有用。 –

0

在Java8可以 「重複」 的註釋,如:

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

這樣一來,後者覆蓋第一個文件的屬性(如果可用)。