1

有没有办法在 Grails 集成测试中获取应用程序 URL?

URL 应包括:

  • 服务器名称
  • 港口
  • 上下文路径

例如“本地主机:8080/appname”

我不介意协议是否包含在内。

我正在寻找一种在运行时执行此操作的方法 - 将 URL 硬编码为 egConfig.groovy是没有用的。

4

4 回答 4

2

这是一种丑陋的做法。

首先,将grailsLinkGeneratorbean 注入到集成测试中:

def grailsLinkGenerator

然后,自己构建服务器 URL:

grailsLinkGenerator.serverBaseURL + ':' + 
        (System.properties['server.port']?:'8080') +
        grailsLinkGenerator.contextPath
于 2013-10-09T13:34:18.120 回答
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

尝试这个..,。

于 2013-10-09T10:07:57.783 回答
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/"
  }
}

在 buildconfig.groovy 上

grails.server.port.http="8080" 

将您的应用程序 grails -Dgrails.env=test test-app 运行为在您选择的任何端口中运行服务器,例如 8188 ,8181

grails -Dgrails.port = 8181 test-app

于 2013-10-09T11:29:12.930 回答
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")
...
于 2015-07-23T11:59:33.353 回答