2017-03-16 107 views

回答

0

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") 
    } 
} 
+0

thx爲答案。但是,如何將它附加到正在運行的應用程序在'localhost:8080'上? – zatziky

+0

你在談論功能測試而不是集成測試。 – Joe

+0

我在說這兩個測試。在Grails中,功能測試在技術上也是集成測試,因爲它們也用'@ Integration'註釋......但是非常感謝您試圖幫助我。 :D也許我應該概括一下我的問題:「我如何再次運行任何代碼來運行Grails服務器?」。你看到我需要什麼嗎? – zatziky

0

也許你想找的功能測試不集成測試。 Grails 3爲功能測試增加了更好的支持。請參閱以下鏈接。

Grails 3 Functional Tests