在 Amazon 和 Codeship 上测试失败

Test fails on Amazon and Codeship

在 Groovy 中使用 Dropbox 和 Google Drive 构建应用程序,测试 运行 很好。

代码:

 def "It instantiates and configures the googleAuthorizationCodeFlowBuilder component"() {
         given:
         def ctx = grailsApplication.mainContext
         GoogleAuthorizationCodeFlow.Builder builder = ctx.googleAuthorizationCodeFlowBuilder

         expect:
 }

然而,当 运行在 Codeship 和 Amazon 上运行时,测试失败:

Message: Error creating bean with name 'googleAuthorizationCodeFlowBuilder': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow$Builder]: Constructor threw exception; nested exception is java.lang.NullPointerException

Line | Method ->> 285 | autowireConstructor in org.springframework.beans.factory.support.ConstructorResolver

| 1075 | autowireConstructor in org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory

/* stuff */

org.apache.catalina.core.ContainerBase$StartChild | 1549 | call in '' | 262 | run . . . . . . . . . . . . . . in java.util.concurrent.FutureTask | 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor | 615 | run . . . . . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker ^ 745 | run in java.lang.Thread

Caused by BeanInstantiationException: Could not instantiate bean class [com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow$Builder]: Constructor threw exception; nested exception is java.lang.NullPointerException ->> 163 | instantiateClass in org.springframework.beans.BeanUtils

| 121 | instantiate in org.springframework.beans.factory.support.SimpleInstantiationStrategy | 277 | autowireConstructor . . . . . . in org.springframework.beans.factory.support.ConstructorResolver | 1075 | autowireConstructor in org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory | 979 | createBeanInstance . . . . . . in '' | 487 | doCreateBean in '' | 123 | doCreateBean . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor

/* more stacktrace */

| 615 | run . . . . . . . . . . . . . . in java.util.concurrent.ThreadPoolExecutor$Worker ^ 745 | run in java.lang.Thread

googleAuthorizationflowBuilder bean 仅创建一个 GoogleAuthorizationCodeFlow.Builder(我使用 Google java API)。

编辑 1:我通过修改 doWithSpring 在我的机器上重现了错误:

def doWithSpring = {
        def config = application.config
        String googleSecret = getConfigValue(config, 'storage.google.secret')
        String googleAppId = null//getConfigValue(config, 'storage.google.appId')
        String appName = getConfigValue(config, 'storage.appName')

        if (!ConfigUtils.optional(config,'storage.google.disabled')) {
            googleNetHttpTransport(NetHttpTransport)
            googleJsonFactory(JacksonFactory)
            googleCredentialsBuilder(GoogleCredential.Builder) {
                transport = googleNetHttpTransport
                jsonFactory = googleJsonFactory
            }
       googleAuthorizationCodeFlowBuilder(GoogleAuthorizationCodeFlow.Builder, ref('googleNetHttpTransport'), ref('googleJsonFactory'),
                googleAppId, googleSecret, [DRIVE])
        }
    }

编辑 2:似乎错误发生在 AppId 为 null 时...问题是为什么它在云端而不是在我的机器上为 null...

好的,看来是在Codebase项目上没有设置用于初始化Config.groovy值的环境变量,导致上述错误。

缺少的环境变量已在 Codeship 上初始化,现在一切正常。