Grails 在集成测试中获取服务器URL?
有没有办法在Grails集成测试中获取应用程序URL URL应包括:Grails 在集成测试中获取服务器URL?,grails,integration-testing,Grails,Integration Testing,有没有办法在Grails集成测试中获取应用程序URL URL应包括: 服务器名 港口 上下文路径 e、 g.“本地主机:8080/appname” 我不介意协议是否包括在内 我正在寻找一种在运行时实现这一点的方法-将URL硬编码到例如Config.groovy中是没有用的。测试环境没有添加到配置文件中,只需添加测试环境并将服务器URL像这样放在那里 environments { development { grails.logging.jul.usebridge = true
- 服务器名
- 港口
- 上下文路径
我正在寻找一种在运行时实现这一点的方法-将URL硬编码到例如
Config.groovy
中是没有用的。测试环境没有添加到配置文件中,只需添加测试环境并将服务器URL像这样放在那里
environments {
development {
grails.logging.jul.usebridge = true
grails.serverURL = "http://test2mkb.co.in"
}
production {
grails.logging.jul.usebridge = false
grails.serverURL = "http://test2mkb.co.in"
}
test {
grails.serverURL = "http://test2mkb.co.in"
}
}
然后在测试中
def grailsApplication
...
String serverURL = grailsApplication.config.grails.serverURL
编辑
import org.codehaus.groovy.grails.web.mapping.LinkGenerator
...
LinkGenerator grailsLinkGenerator
...
String serverURL = grailsLinkGenerator.serverBaseURL
尝试此..,.与上面相同,但请明确指定端口
关于buildconfig.groovy
grails.server.port.http="8080"
运行您的应用程序作为
grails-Dgrails.env=test
testapp
在您选择的任何端口(如8188、8181)中运行服务器
grails-Dgrails.port=8181测试应用程序
下面是一个丑陋的方法
首先,将grailsLinkGenerator
bean注入集成测试:
def grailsLinkGenerator
然后,自己构造服务器URL:
grailsLinkGenerator.serverBaseURL + ':' +
(System.properties['server.port']?:'8080') +
grailsLinkGenerator.contextPath
我对Grails 3.x的看法基于其他答案,并巧妙地融入了易于重用的特点: BaseUrl.groovy
/**
* Exposes the {@code baseUrl} property for integration and functional tests, i.e. {@code http://<host>:<port><contextPath><path>}, where
* {@code path} is optionally provided by implementing classes by overriding the {@link #getPath()} method.<br><br>
*
* <b>Please note</b> that this trait requires {@code grailsApplication} to be present on the implementing class. This may be achieved
* by annotating the test class with {@code @TestFor(...)}.
*
* @author Michael Jess (http://stackoverflow.com/users/1821301/michael-jess)
*
*/
trait BaseUrl {
/**
* Retrieves the grails link generator
*
* @return the {@code grailsLinkGenerator} bean
*/
private def getGrailsLinkGenerator() {
assert grailsApplication, "grailsApplication not set, did you forget the @TestFor annotation?"
grailsApplication.mainContext.getBean("grailsLinkGenerator")
}
/**
* Returns the server base URL as provided by the link generator
*
* @return The server base url ({@code http://<host>:<port>})
*/
private def getServerBaseUrl() {
getGrailsLinkGenerator().serverBaseURL
}
/**
* Returns the context path as provided by the link generator
*
* @return The context path (e.g. {@code /<appName>} pre-grails 3.x)
*/
private def getContextPath() {
getGrailsLinkGenerator().contextPath
}
/**
* Returns the grails application base URL
*
* @return The grails application base URL ({@code http://<host>:<port><contextPath><path>})
*/
def getBaseUrl() {
getServerBaseUrl() + getContextPath() + getPath()
}
/**
* Returns the path of the current controller under test. Should be overridden by test classes.
*
* @return The controller path fragment
*/
def getPath() { "" }
}
然后在测试用例中:
...
get("$baseUrl/you/name/it")
...
很好,谢谢-但是它如何知道服务器端口和上下文路径呢?
grails.serverURL
在我的例子中是服务器urlhttp://test2mkb.co.in
是我的示例应用程序url。在您的情况下http://localhost:8080/appname
。和grailsApplication.config.grails.serverURL
为您提供http://localhost:8080/appname
我对问题进行了编辑,以明确我希望在运行时获取这些值,而不是硬编码它们。很抱歉造成混淆。谢谢,我们将尝试一下……尽管链接生成器尚未初始化。也许它是一个SpringBean?有可能动态地这样做吗?端口并不总是相同的。grails-Dgrails.server.port=8181运行appYes,但您已将端口硬编码到配置中,因此可能URL会不正确。此外,contextpath不包括在内。我明白了,您可以省略:8080移植的内容。。。它不会产生任何问题,请参考[此答案][1]
@TestFor(MyController)
class MyControllerSpec extends Specification implements BaseUrl {
@Override
def getPath() { "/api/myController" }
...
...
get("$baseUrl/you/name/it")
...