Unit testing Grails-控制器的单元测试&x27;使用reCaptcha插件的方法

Unit testing Grails-控制器的单元测试&x27;使用reCaptcha插件的方法,unit-testing,grails,configuration,controller,recaptcha,Unit Testing,Grails,Configuration,Controller,Recaptcha,我想对以下方法进行单元测试: def handleEmailSharing = { EmailSharingCommand esc -> if (params.send) { def recaptchaOK = true if (!recaptchaService.verifyAnswer(session, request.getRemoteAddr(), params)) { recaptch

我想对以下方法进行单元测试:

def handleEmailSharing = { EmailSharingCommand esc ->
        if (params.send) {
            def recaptchaOK = true
            if (!recaptchaService.verifyAnswer(session, request.getRemoteAddr(), params)) {
                recaptchaOK = false
            }

         }
...
这就是我尝试的方式:

import com.megatome.grails.RecaptchaService

class EmailSharerControllerTests extends ControllerUnitTestCase {

    protected void setUp() {
     controller.recaptchaService = new RecaptchaService()
   }

void testHandleEmailSharingSendAndSuccess() {
       mockCommandObject(EmailSharingCommand)
        def emailSharingCommand = new EmailSharingCommand(from: "acorrect@emailaddress.fr", 
                                                          to: "   anothercorrect@emailaddress.fr   ,    whichis@notalone.it   ", 
                                                          cc: "", 
                                                          bcc:"someonein@bcc.com.br",
                                                          trimmedListOfToRecipients: ["anothercorrect@emailaddress.fr", "whichis@notalone.it"])
        emailSharingCommand.validate()
}
controller.handleEmailSharing(emailSharingCommand)
但我得到了以下错误:

Cannot get property 'recaptcha' on null object
java.lang.NullPointerException: Cannot get property 'recaptcha' on null object
    at com.megatome.grails.RecaptchaService.getRecaptchaConfig(RecaptchaService.groovy:33)
    at com.megatome.grails.RecaptchaService.this$2$getRecaptchaConfig(RecaptchaService.groovy)
    at com.megatome.grails.RecaptchaService$this$2$getRecaptchaConfig.callCurrent(Unknown Source)
    at com.megatome.grails.RecaptchaService.isEnabled(RecaptchaService.groovy:100)
    at com.megatome.grails.RecaptchaService$isEnabled.callCurrent(Unknown Source)
    at com.megatome.grails.RecaptchaService.verifyAnswer(RecaptchaService.groovy:81)
    at com.megatome.grails.RecaptchaService$verifyAnswer.call(Unknown Source)
    at bankemist.personalcreditcomparator.EmailSharerController$_closure2.doCall(EmailSharerController.groovy:49)
    at bankemist.personalcreditcomparator.EmailSharerControllerTests.testHandleEmailSharingSendButtonButMissingFromAndTo(EmailSharerControllerTests.groovy:49)
这很奇怪,因为它基本上说我的
org.codehaus.groovy.grails.commons.ConfigurationHolder
为空,或者它包含的
recaptcha
对象。
RecaptchaService.groovy的调用行33是:

if (ConfigurationHolder.config.recaptcha) {...}
或者(最后一个:))我的RecaptchaConfig.groovy就是这样设置的:

recaptcha {
    // These keys are generated by the ReCaptcha service
    publicKey = "xxx"
    privateKey = "xxx"

    // Include the noscript tags in the generated captcha
    includeNoScript = true
}

mailhide {
    // Generated by the Mailhide service
    publicKey = ""
    privateKey = ""
}

environments {
  development {
    recaptcha {
      // Set to false to disable the display of captcha
      enabled = true

      // Communicate using HTTPS
      useSecureAPI = false
    }
  }
  test {
    recaptcha {
      // Set to false to disable the display of captcha
      enabled = true

      // Communicate using HTTPS
      useSecureAPI = false
    }
  }
我无法解决这个问题。
我尝试将configurationHolder导入测试文件,但它没有改变任何内容。非常感谢您的帮助

单元测试将没有ConfigurationHolder,因为它们不是在运行框架内执行的。在单元测试中,最好的选择是模拟RecaptChasService,因此在测试方法中如下所示:

def recapMock = mockFor(RecaptchaService)
recapMock.demand.verifyAnswer(1..1) { session, remoteAddr, params ->
     return true // Or false if you want it to fail in your test
}
controller.recaptchaService = recapMock.createMock()
// Then run your test
controller.handleEmailSharing(emailSharingCommand)

单元测试将没有ConfigurationHolder,因为它们不是在运行的框架内执行的。在单元测试中,最好的选择是模拟RecaptChasService,因此在测试方法中如下所示:

def recapMock = mockFor(RecaptchaService)
recapMock.demand.verifyAnswer(1..1) { session, remoteAddr, params ->
     return true // Or false if you want it to fail in your test
}
controller.recaptchaService = recapMock.createMock()
// Then run your test
controller.handleEmailSharing(emailSharingCommand)

您应该注入RecaptChasService并模拟verifyAnswer,以返回您想要为测试用例返回的任何内容

     def recaptchaServiceMock = mockFor(recaptchaService)

            recaptchaServiceMock.demand.verifyAnswer() {Session session, 
                                                        Map params->
                return true
            }

      currentService.recaptchaService = recaptchaServiceMock.createMock()

您应该注入RecaptChasService并模拟verifyAnswer,以返回您想要为测试用例返回的任何内容

     def recaptchaServiceMock = mockFor(recaptchaService)

            recaptchaServiceMock.demand.verifyAnswer() {Session session, 
                                                        Map params->
                return true
            }

      currentService.recaptchaService = recaptchaServiceMock.createMock()