700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > X-Mas Musings –在Grails集成测试中不要使用随机服务器端口

X-Mas Musings –在Grails集成测试中不要使用随机服务器端口

时间:2022-05-23 06:08:29

相关推荐

X-Mas Musings –在Grails集成测试中不要使用随机服务器端口

对许多人来说,十二月是反思或思考的时期。 所以我决定在去年的事情和想法,以反映- 每一天 ,直到圣诞节。 这是第四天

对于Grails集成测试,了解应用程序当前在哪个端口上运行非常有用。

Spring Boot以及因此建立在它上面的Grails都通过一个名为local.server.port的属性公开了启动时随机选择的端口。

当专门针对Grails进行谷歌搜索时,通常会出现在mrhaki的Grails善意:集成测试页面中使用随机服务器端口 -Grails善意的极好来源-该书清楚地显示了如何使用@Value来获取local.server.port属性的值。 。

在我自己的示例中,您可以在下面看到它的运行情况。

import grails.plugins.rest.client.RestBuilderimport grails.plugins.rest.client.RestResponseimport grails.test.mixin.integration.Integrationimport org.springframework.beans.factory.annotation.Valueimport spock.lang.Specification@Integrationclass 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}}

去年某个时候,我意识到:我根本不需要。

@Integrationclass 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)所说:

任何足够高级的注释都无法与魔术区分开。

如此真实。

翻译自: //12/x-mas-musings-not-use-random-server-port-grails-integration-tests.html

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。