简体   繁体   中英

Grails 3.3.11 Integration Test (GrailsApplication)

I am now in trouble with the configuration variable GrailsApplication in my Integration Tests. I don't know why, but, I am not managing to get its value when testing my api. I am using 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.

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. The IDE shows it as being uninitialized but running the tests, it gets the correct value assigned. I also have my test classes annotated with @Integration(applicationClass = Application.class) .

Here's how I get the URL to point against:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM