简体   繁体   English

Grails 3.3.11 集成测试(GrailsApplication)

[英]Grails 3.3.11 Integration Test (GrailsApplication)

I am now in trouble with the configuration variable GrailsApplication in my Integration Tests.我现在在集成测试中遇到配置变量 GrailsApplication 的问题。 I don't know why, but, I am not managing to get its value when testing my api.我不知道为什么,但是,在测试我的 api 时我没有设法获得它的价值。 I am using Grails 3.3.11.我正在使用 Grails 3.3.11。 The value of the variable is being null and, due to it, I can't authenticate to perform the tests.变量的值为空,因此,我无法进行身份验证以执行测试。 I would appreciate your help.我会很感激你的帮助。 I am using 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
        }
    }

I've been upgrading a Grails 2.x app to version 3.3.11 and just referencing the (provided) variable serverPort worked for me.我一直在将 Grails 2.x应用程序升级到3.3.11版,并且只是引用(提供的)变量serverPort对我serverPort The IDE shows it as being uninitialized but running the tests, it gets the correct value assigned. IDE 将其显示为未初始化,但在运行测试时,它会获得正确的分配值。 I also have my test classes annotated with @Integration(applicationClass = Application.class) .我还用@Integration(applicationClass = Application.class)注释了我的测试类。

Here's how I get the URL to point against:以下是我如何获取指向的 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