2015-05-13 74 views
2

我有一個服務器,用Java編寫,這是我的應用程序,我想對它運行一些測試。我使用gradle來管理依賴關係並構建任務和內容,所以我也想使用它。我需要啓動服務器,然後運行我的單元測試,這些測試會對它做出一堆HTTP請求,然後在測試完成時甚至關閉服務器。所以我嘗試添加到我的build.gradle test.dependsOn(jettyRunWar)(jettyRunWar運行服務器),但我認爲這太簡單了,因爲gradle test永遠不會從jettyRunWar返回以進行測試。我可以將它連接起來,這樣gradle將啓動服務器,然後運行測試?如何使用gradle測試服務器?

+0

你有沒有考慮[使用junit](http://stackoverflow.com/questions/20707017/how-to-run-junit-tests-with-gradle),[或者像這樣](http:// java .dzone.com/articles/gradle-goodness-running-single) – Paxic

+0

對,我試圖使用JUnit,但是如果我嘗試運行我的服務器,以便我可以運行我的測試,那麼Gradle不會從任務返回運行服務器,以便它永遠不會運行測試。 –

回答

1

是啊!雅得添加

jettyRunWar { 
    daemon = true 
} 

到您的build.gradle。然後,如果你想在年底關閉服務器啊,你必須投這也:

stopPort = 1234 
stopKey = 'stopKey' 
// gradle is luser. Full story (and code copied from): https://issues.gradle.org/browse/GRADLE-2263 
import org.gradle.api.plugins.jetty.internal.Monitor 
[jettyRun, jettyRunWar]*.doLast { 
    /** 
    * THIS IS A WORKAROUND! THE CURRENT VERSION OF THIS TASK DOESN'T START A WATCHER IN DAEMON MODE 
    * 
    * If starting the monitor fails, it may be because the jetty task was updated to fix this issue 
    * When that happens, we shouldn't need the custom task any more 
    * 
    * Copied From: AbstractJettyRunTask 
    */ 
    if (getStopPort() != null && getStopPort() > 0 && getStopKey() != null) { 
     Monitor monitor = new Monitor(getStopPort(), getStopKey(), server.getProxiedObject()); 
     monitor.start(); 
    } 
} 

確保你有關於「gradle這個是luser」的評論。