2011-10-25 34 views
2

情況:我有一個MyController類,它與一些外部WebServices一起工作。如何從jUnit測試中啓動maven過濾?

public class MyController { 
    private String integrationWebServiceURL; 
} 

這個類的web服務URL在描述符配置控制器豆(的applicationContext.xml)期間傳遞

<bean id="myController" class="com.mypath.MyController"> 
    <property name="integrationWebServiceURL" value="${integration.web.service.url}"/> 
</bean> 

值是動態的,實際值被存儲在屬性文件中應用。性能

integration.web.service.url=${pom.integration.web.service.url} 

但它不是終點 - 真正的VA lue存儲在maven項目文件中(pom.xml),其中filtering = true。

<pom.integration.web.service.url>http://mywebservices.com</pom.integration.web.service.url> 

所以,當我們用MVN從pom.xml的安裝測試值複製到適當的application.properties佔位符,然後測試我的類的工作就好了。

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"/applicationContext.xml"}) 
public class MyControllerTest { 

} 

問題:我需要從IDE啓動我的測試,能夠與不同的設置,以發揮和使用IDE的調試功能。但是如果我簡單地從IDE開始這個測試,而沒有初步的Maven構建 - 比我的web服務地址將簡單地從application.properties獲取並且等於「$ {pom.integration.web.service.url}」(例如,Maven過濾的過程不會測試前不工作)。如何調整Maven,Spring或jUnit以從pom.xml中提取我的值?

注意:我知道我可以簡單地在由test-class使用的application.properties或applicationContext.xml文件中明確設置此值,但我需要從pom.xml中提取此值。

+0

如果您使用Eclipse,你嘗試過M2Eclipse或M2E插件嗎? – Ralph

+0

爲什麼你需要從POM中獲得價值?或者說,我的意思是,爲什麼POM是一個適合存儲這些信息的地方?我建議將這些數據保存在可交付工件外側的屬性文件中,並僅在運行時訪問它。然後,您可以獲得屬性文件的一個版本,僅在測試類路徑中提供,該文件特定於測試。 –

+0

@Ralph IntelliJ IDEA是我的IDE。 – dim1902

回答

0

只需使用Maven的運行,如:

mvn test 

則應全部用POM變量過濾去。 您可以擁有特定屬性文件的testResources。或者是一個applicationContext-test.xml。

+0

我認爲OP的需求是讓IDE在從IDE進行clean + build(即非Maven構建)之後進行測試。 –

0

最好的解決方案是使用Maven感知的IDE,並且在源文件必須被創建時運行mvn copy-resources。對於Eclipse,請嘗試m2e,對於IDEA,Maven插件也應該這樣做。

如果不是出於某種原因的選項,您可以手動在通用測試代碼的靜態代碼塊運行目標,例如(所以它總是執行一次):

static { 
    Process p = Runtime.getRuntime().exec("mvn copy-resources"); 
    IOUtils.copy(p.getInputStream(), System.out); 
    p.waitFor(); 
} 
相關問題