2017-01-13 67 views
4

我有一個Spring Boot 1.4.3項目。 在test/resources文件夾中我有兩個屬性文件,比如說 a-test.propertiesb-test.propertiesSpring Boot加載所有可用的屬性文件,無論我的@TestPropertySource批註

測試類被註釋如下:

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
@TestPropertySource(locations = "classpath:a-test.properties") 

然而,我看到在我的測試也從b-test.properties屬性被加載(I通過一個簡單的打印輸出驗證了這一點)。

爲什麼?我怎樣才能防止這一點?


實施例從我測試提取

@RunWith(SpringRunner.class) 
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 
@TestPropertySource(locations = "classpath:base-test.properties", inheritProperties=false) 
public class EmailServiceContextBasedTest { 

    @SpyBean 
    public JavaMailSender javaMailSender; 

    @Before 
    public void setUp() throws Exception { 

     System.out.println(
      ((JavaMailSenderImpl)javaMailSender).getPassword() 
     ); 
     System.out.println(
      ((JavaMailSenderImpl)javaMailSender).getJavaMailProperties() 
     ); 
    } 


    @Test 
    public void test() throws Exception { 
     // do nothing 
    } 

} 

其中a-test.properties是:

spring.mail.host=smtp.gmail.com 
spring.mail.port=587 
[email protected] 
spring.mail.password=password 
spring.mail.properties.mail.smtp.auth=false 
spring.mail.properties.mail.smtp.starttls.enable=false 

b-test.properties

spring.mail.host=smtp.gmail.com 
spring.mail.port=587 
[email protected] 
spring.mail.password=myPassword 
spring.mail.properties.mail.smtp.auth=true 
spring.mail.properties.mail.smtp.starttls.enable=true 
+1

您如何閱讀房產?你能添加一些代碼來證明你正在描述的行爲嗎? – dimitrisli

+0

@dimitrisli是的,當然!我附上了一個示例 –

+0

@mat_boy請提供您獲得的輸出和您期望的輸出。你的例子只會打印與其中一個屬性文件相關的密碼(在我的例子中它是'a-test.properties')。那麼你是如何驗證'也是從b-test.properties中加載的屬性'?! – Omid

回答

0

overrride它,我找到了問題的根源。

我在一類以下

@SpringBootApplication 
@EnableAutoConfiguration 
@PropertySource("classpath:a-test.properties") 
public class TestApplication { 

    public static void main(final String... args) { 
     SpringApplication.run(TestApplication.class, args); 
    } 
} 

那不是在測試稱爲定義爲test文件夾,但我想在DefaultEmailServiceContextBasedTest類執行測試時被實例化默認由Spring啓動與註釋

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) 

所以,問題是,我用@PropertySource。我不知道爲什麼,但用@TestPropertySource替換了註釋解決了衝突問題。

3

隨着SpringBootTest註釋它會自動彈出與文檔有關的彈簧引導應用配置

可以在運行基於Spring Boot的測試的測試類上指定的註釋。提供以下功能之上的常規了Spring TestContext框架:

  • 用途SpringBootContextLoader作爲默認的ContextLoader沒有具體@ContextConfiguration(裝載機= ...)定義爲當。
  • 未使用嵌套的@Configuration時,自動搜索@SpringBootConfiguration,並且未指定明確的類。
  • 允許使用properties屬性定義自定義環境屬性。
  • 爲不同的Web環境模式提供支持,包括啓動在定義的或隨機的端口上偵聽的完全運行容器的功能。
  • 註冊TestRestTemplate bean以用於使用完全運行的容器的Web測試。

,所以我想你加載其他地方的其他屬性,但事實是@TestPropertySource(locations = "classpath:a-test.properties", inheritProperties=false)只會加載a-test.properties確實如此。

下面是一個簡單的測試:

  • a-test.properties enter image description here
  • b-test.properties enter image description here

而且隨着@TestPropertySource annotat離子,你仍然可以更改覆蓋性能測試與properties屬性運行之前,

對於你的問題,你可以像@TestPropertySource(locations = "classpath:b-test.properties", properties = {"spring.mail.host=smtp.gmail.com", "spring.mail.port=587", "[email protected]" ......})

0

您正在混合使用測試註釋的應用程序級別註釋。

@SpringBootApplication是應用程序級別註釋,只能與@PropertySource註釋一起使用。

因此,用@TestPropertySource代替@PropertySource與沒有註釋相似。

您需要修復您的結構。

@SpringBootApplication註解類移動到主包並添加註釋@PropertySource

不確定爲什麼你有兩個資源具有相同的屬性,但理想情況下,你應該有一個應用程序和測試。

讓我們假設a-test.properties是一個資源,@PropertySource爲您的應用程序和b-test.properties是資源@TestPropertySource爲您的測試。

當您使用@TestPropertySourceb-test.properties運行測試類時,它優先於從類路徑加載的任何其他屬性文件。

當測試Spring Boot應用程序時,通常不需要這樣做。 Spring Boot's @ *測試註釋將自動搜索您的主要配置 ,只要您沒有明確定義配置。

搜索算法從包含測試 的包中運行,直到找到@SpringBootApplication或@SpringBootConfiguration 帶註釋的類爲止。只要你用合理的方式構建你的代碼,你的主要配置通常就可以找到。

因此,您的班級@SpringBootApplicationwebEnvironment將爲您的測試加載嵌入式容器。

更多關於測試框架如何找到配置的信息。

http://docs.spring.io/spring-boot/docs/1.4.0.M3/reference/htmlsingle/#boot-features-testing-spring-boot-applications-detecting-config

,如果你想使用特殊配置的測試,您可以使用@SpringBootTest(classes=MyTestApplication.class)

更多在這裏的java文檔。

http://docs.spring.io/spring-boot/docs/current/api/org/springframework/boot/test/context/SpringBootTest.html

0

最容易做的,你需要的是設定分佈,使得:

@ActiveProfiles( 「測試」)

然後簡單地寫具有所需性能的application-test.properties文件爲了運行你的測試。

相關問題