【问题标题】:Run Integration Tests agains a running Server针对正在运行的服务器运行集成测试
【发布时间】:2017-08-07 15:45:56
【问题描述】:

如何针对正在运行的服务器运行 Grails 集成测试?

我在网上搜索过,但建议日期为 2010 年和 Grail 1.3.x...

【问题讨论】:

    标签: testing grails grails-3.0 grails-3.1


    【解决方案1】:

    Grails 3 集成测试将针对正在运行的服务器运行。您需要定义一个测试环境 application.yml 并将@Integration 注解添加到您的类中。

    import grails.test.mixin.integration.Integration
    import spock.lang.Specification
    
    @Integration
    class LookupServiceIntegrationSpec extends Specification {
    
        def lookupService
    
        def setup() {
        }
    
        def cleanup() {
        }
    
        void "test database connection"() {
    
            when:
            def row = lookupService.testMethod()
    
            then:
            println("Row one = "+row.one)
            row.one == 1
        }
    }
    

    这是一个简单的示例服务:

    import grails.transaction.Transactional
    import groovy.sql.Sql
    
    @Transactional
    class LookupService {
    
        def dataSource
    
        def testMethod() {
            Sql sql = new Sql(dataSource)
            sql.firstRow("SELECT 1 AS one")
        }
    }
    

    【讨论】:

    • 感谢您的回答。但是如何将其附加到正在运行的应用程序,例如在localhost:8080?
    • 您谈论的是功能测试而不是集成测试。
    • 我说的是这两个测试。在 Grails 中,功能测试在技术上也是集成测试,因为它们也使用 @Integration... 进行注释,但非常感谢您尝试帮助我。 :D 也许我应该概括我的问题:“我怎样才能在运行中的 Grails 服务器上运行任何代码?”。你看到我需要什么了吗?
    【解决方案2】:

    也许您正在寻找功能测试而不是集成测试。 Grails 3 为功能测试添加了更好的支持。请参阅下面的链接。

    Grails 3 Functional Tests

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2020-11-07
      • 2011-10-27
      • 2021-01-31
      • 2022-08-03
      • 1970-01-01
      • 2016-06-12
      • 1970-01-01
      相关资源
      最近更新 更多