X-Mas Musings –在Grails集成测试中不要使用随机服务器端口
對許多人來說,十二月是反思或思考的時期。 所以我決定在去年的事情和想法,以反映- 每一天 ,直到圣誕節。 這是第四天
對于Grails集成測試,了解應用程序當前在哪個端口上運行非常有用。
Spring Boot以及因此建立在它上面的Grails都通過一個名為local.server.port的屬性公開了啟動時隨機選擇的端口。
當專門針對Grails進行谷歌搜索時,通常會出現在mrhaki的Grails善意:集成測試頁面中使用隨機服務器端口 -Grails善意的極好來源-該書清楚地顯示了如何使用@Value來獲取local.server.port屬性的值。 。
在我自己的示例中,您可以在下面看到它的運行情況。
import grails.plugins.rest.client.RestBuilder import grails.plugins.rest.client.RestResponse import grails.test.mixin.integration.Integration import org.springframework.beans.factory.annotation.Value import spock.lang.Specification@Integration class SomeIntegrationSpec extends Specification {@Value('${local.server.port}')Integer serverPortvoid "health check works"() {when:String url = "http://localhost:${serverPort}/example/health"def response = new RestBuilder().get(url)then:response.status == 200} }去年某個時候,我意識到:我根本不需要。
@Integration class SomeIntegrationSpec extends Specification {// no serverPort!void "health check works"() {when:String url = "http://localhost:${serverPort}/example/health"def response = new RestBuilder().get(url)then:response.status == 200} }WAT? 沒有serverPort屬性-您仍在"http://localhost:${serverPort}/example/health"嗎?
Jip,至少在Grails 3.3.0中具有此功能,即使用corrct值初始化的確切屬性Integer serverPort ,是通過@Integration批注直接添加到測試類的 -特別是:其AST轉換幫助器類。
正如英國小說作家亞瑟·克拉克 ( Arthur C. Clarke)所說:
任何足夠高級的注釋都無法與魔術區分開。
如此真實。
翻譯自: https://www.javacodegeeks.com/2017/12/x-mas-musings-not-use-random-server-port-grails-integration-tests.html
總結
以上是生活随笔為你收集整理的X-Mas Musings –在Grails集成测试中不要使用随机服务器端口的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring JPA数据+Hiberna
- 下一篇: 解除腾讯视频会员自动续费