有没有办法在Grails集成测试中获取应用程序URL?
网址应包含:
e.g。 “本地主机:8080 /应用程序的名字”
我不介意是否包括协议。
我正在寻找一种在运行时执行此操作的方法 - 将URL硬编码到例如Config.groovy
没用。
答案 0 :(得分:2)
这是一种丑陋的做法。
首先,将grailsLinkGenerator
bean注入集成测试:
def grailsLinkGenerator
然后,自己构建服务器URL:
grailsLinkGenerator.serverBaseURL + ':' +
(System.properties['server.port']?:'8080') +
grailsLinkGenerator.contextPath
答案 1 :(得分:1)
测试环境未添加到配置文件中,只需添加测试环境并将服务器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
试试这个..,。
答案 2 :(得分:0)
environments {
development {
grails.logging.jul.usebridge = true
grails.serverURL = "http://test2mkb.co.in:8080/"
}
production {
grails.logging.jul.usebridge = false
grails.serverURL = "http://test2mkb.co.in:8080/"
}
test {
grails.serverURL = "http://test2mkb.co.in:8080/"
}
}
grails.server.port.http="8080"
将您的应用运行为
grails -Dgrails.env=test
test-app
在您选择的任何端口中运行服务器,如8188,8181
grails -Dgrails.port = 8181 test-app
答案 3 :(得分:0)
我对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() { "" }
}
只需实现它:
@TestFor(MyController)
class MyControllerSpec extends Specification implements BaseUrl {
@Override
def getPath() { "/api/myController" }
...
然后在测试用例中:
...
get("$baseUrl/you/name/it")
...