【问题标题】:Get server URL in integration test?在集成测试中获取服务器 URL?
【发布时间】:2015-07-23 19:59:33
【问题描述】:

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

网址应包括:

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

例如"localhost:8080/appname"

我不介意是否包含协议。

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

【问题讨论】:

标签: grails integration-testing


【解决方案1】:

这是一种丑陋的做法。

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

def grailsLinkGenerator

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

grailsLinkGenerator.serverBaseURL + ':' + 
        (System.properties['server.port']?:'8080') +
        grailsLinkGenerator.contextPath

【讨论】:

    【解决方案2】:

    配置文件中没有添加测试环境,只需添加测试环境并将服务器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
    

    试试这个..,.

    【讨论】:

    • 很好,谢谢 - 但它如何知道服务器端口和上下文路径?
    • grails.serverURL 在我的情况下是服务器 url http://test2mkb.co.in 是我的示例应用程序 url。在你的情况下http://localhost:8080/appnamegrailsApplication.config.grails.serverURL 给你http://localhost:8080/appname
    • 我已经编辑了这个问题,以明确表示我想在运行时获取这些值,而不是硬编码它们。很抱歉造成混乱。
    • 谢谢,会试一试
    • ...虽然 LinkGenerator 没有初始化。也许它是一个 Spring bean?
    【解决方案3】:

    同上,但明确提及端口

    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=testtest-app 在您选择的任何端口上运行服务器,例如 8188 ,8181

    grails -Dgrails.port = 8181 test-app

    【讨论】:

    • 是否可以动态执行此操作?端口并不总是相同的。
    • grails -Dgrails.server.port = 8181 运行应用
    • 是的,但是您已将端口硬编码到配置中,因此推测该 URL 将不正确。也不包括上下文路径。
    • 我明白了,你可以省略 :8080 移植的东西......它不会产生任何问题
    【解决方案4】:

    我对 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")
    ...
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-03-26
      • 2019-03-15
      • 2019-06-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-31
      • 2021-01-11
      相关资源
      最近更新 更多