2014-09-26 64 views
1

我已經在每個測試中都放了一個@IntegrationTest註解,有時我會用它來爲環境添加屬性。在一起運行所有測試時,似乎只使用第一個@IntegrationTest註釋中遇到的屬性,因此一些測試失敗。有沒有辦法強制重新加載這些屬性?@Integration測試套件中的屬性不會被重新加載

這是我使用的例子:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes=TestApplication.class) 
@WebAppConfiguration 
@IntegrationTest("some.property=true") 
public class SomeIntegrationTest { 
+0

你如何確定使用哪個'@ IntegrationTest'屬性,即哪個測試類首先被加載? – Paul 2014-12-04 18:59:22

回答

3

Spring Boot被測試的應用程序只對所有測試啓動一次,這對於測試性能來說是一件好事。如果你想開始用不同的屬性設置另一個應用程序,你必須寫又一個春天啓動的應用程序類是這樣的:

@Configuration 
@EnableAutoConfiguration 
public class MetricsTestApplication { 

    public static void main(String[] args) { 
     SpringApplication.run(MetricsTestApplication.class, args); 
    } 

} 

在集成測試,您引用其他類。另外,你必須設置不同的端口比第一應用程序,你可以添加從第一個應用程序的屬性不同的屬性:

@RunWith(SpringJUnit4ClassRunner.class) 
@SpringApplicationConfiguration(classes=MetricsTestApplication.class) 
@WebAppConfiguration 
@IntegrationTest({"server.port=8091","batch.metrics.enabled=true"}) 

我們與不同的屬性集MetricsTestApplication下端口現在開始8091.

0

根據API文檔@IntegrationTest註釋是"signifying that the tests are integration tests (and therefore require an application to startup "fully loaded" and listening on its normal ports)"

如果要使用可重新加載的屬性,則應使用EnvironmentTestUtils

例如。

@Autowired 
Environment env; 

@Autowired 
ConfigurableApplicationContext ctx; 

@Before 
public void before() { 
    EnvironmentTestUtils.addEnvironment(ctx, "test.value:myValue"); 
} 

@Test 
public void testGreeting() { 
    assertThat(env.getProperty("test.value"), comparesEqualTo("myValue")); 
} 
+0

以上是整個解決方案嗎?我試圖添加這個,但它似乎並沒有添加屬性。是否有一個應該讓這個運行的類級別的註釋? – 2014-11-10 01:54:23