簡體   English   中英

Grails 3.3.11 集成測試(GrailsApplication)

[英]Grails 3.3.11 Integration Test (GrailsApplication)

我現在在集成測試中遇到配置變量 GrailsApplication 的問題。 我不知道為什么,但是,在測試我的 api 時我沒有設法獲得它的價值。 我正在使用 Grails 3.3.11。 變量的值為空,因此,我無法進行身份驗證以執行測試。 我會很感激你的幫助。 我正在使用 Grails 3.3.11。

package br.com.xxx.id.test.integration

//Imports were moved out to simplify understanding

class IdControllerSpec extends Specification {
    def grailsApplication
    @Value('${local.server.port}')
    Integer serverPort
    String accessToken
    String baseUrl
    JSONObject documentPropertiesForTesting
    JSONObject documentForTesting
    String partTest
    String userTest
    String typeIdTest
    String refreshToken

    void setup(){
        baseUrl = "http://localhost:${serverPort}/cmbid/api/v1"
        partTest = "partTest"
        
    }

void "Saving a new and valid document properties"() {
        when:
            refreshToken = grailsApplication.config.getProperty('refreshToken')
            accessToken = "Bearer " + authenticateXxxAut()
            documentPropertiesForTesting = createNewTestDocumentProperties()
            typeIdTest = documentPropertiesForTesting.get("message").toString().substring(20,52)

        then:
            documentPropertiesForTesting.get("status") == "ok"
            documentPropertiesForTesting.get("message").contains("properly saved!")

        cleanup:
            DocumentProperties.withNewSession {
                def dp = DocumentProperties.findById(typeIdTest)
                dp.delete(flush: true)
            }
    }

def authenticateXxxAut() {
        CloseableHttpClient httpClient = HttpClients.createDefault();
        String response = ""
        try {
            JSONObject responseBody
            println('****************************')
            println(grailsApplication.config.getProperty('aut.newTokenUrl'))
            println(grailsApplication.config.getProperty('refreshToken)'))
            println('****************************')

            def httpPost = new HttpPost(grailsApplication.config.getProperty('aut.newTokenUrl') + grailsApplication.config.getProperty('refreshToken)'))
            CloseableHttpResponse httpResponse = httpClient.execute(httpPost)

            if (httpResponse.getStatusLine().getStatusCode() == 200) {
                responseBody = new JSONObject(EntityUtils.toString(httpResponse.getEntity()))
                response = responseBody.get("access_token")
            } else {
                response = httpResponse.getStatusLine().getStatusCode().toString()
            }
        } catch (Exception e){
            print(e.getLocalizedMessage())
        } finally {
            httpClient.close()
            return response
        }
    }

我一直在將 Grails 2.x應用程序升級到3.3.11版,並且只是引用(提供的)變量serverPort對我serverPort IDE 將其顯示為未初始化,但在運行測試時,它會獲得正確的分配值。 我還用@Integration(applicationClass = Application.class)注釋了我的測試類。

以下是我如何獲取指向的 URL:

def url = "http://localhost:${serverPort}${grailsApplication.config.getProperty('server.contextPath', String, '')}"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM