這裏將測試在Hello World示例章節中創建的示例專案,以演示Spring CLI的測試功能。 按照下麵提到的步驟測試示例專案 -
第1步
在Test
檔夾中創建FirstApplication.groovy
和TestFirstApplication.groovy
,如下所述。
第2步
編譯並運行應用程式以驗證實現的邏輯的結果。
檔: FirstApplication/FirstApplication.groovy -
@RestController
class FirstApplication {
@RequestMapping("/")
String welcome() {
"Welcome to xuhuhu.com"
}
}
檔: FirstApplication/TestFirstApplication.groovy -
class TestFirstApplication {
@Test
void welcomeTest() {
assertEquals("Welcome to xuhuhu.com", new FirstApplication().welcome())
}
}
運行該應用程式
輸入以下命令 -
D:\worksp\springboot-cli/> spring test FirstApplication.groovy TestFirstApplication.groovy
現在,Spring Boot CLI將開始運行,下載所需的依賴項,編譯源代碼和測試檔以及單元測試代碼。可以在控制臺上看到以下輸出 -
Resolving dependencies........................................................
.
Time: 0.457
OK (1 test)
111
執行順序和要點
以下操作由Spring CLI執行 -
@Test
注釋指示CLI下載JUnit 4.12
版本。Spring CLI
使用其元數據自動檢測版本,因為沒有指定任何依賴項。- 最後,它編譯代碼,測試應用程式