How to verify that a static constant value is passed to MailService. Closing sendMail WIthout Do you need a Prefix class in a constant?

My Unit Test graphs puncture MailService with Expando and override the sendMail method. The source code uses the static constant EMAIL_SUBJECT without a prefix. However, when I run the test and delete " Something " from Something.EMAIL_SUBJECT . it cannot claim that mockMailService.subject was passed null . How can I improve the test to "something." not required on EMAIL_SUBJECT?

Test code snippet:

def createMockMailService() {

    def mockMailService = new Expando()

    mockMailService.sendMail = { callable ->

        callable.delegate = mockMailService
        callable.resolveStrategy = Closure.DELEGATE_FIRST
        callable.call()
    }

    mockMailService.subject = { header -> }

    mockMailService
}

void testThis() {
    def mockMailService = createMockMailService()

    mockMailService.subject = { assert it == "value of constant"  }

    Something something = new Something()
    something.mailService = mockMailService

    something.doSomethingThatCallsMailService()
}

Code test:

class Something {

    static def EMAIL_SUBJECT = "value of constant"

    def mailService = new SomeMailThing()

    def doSomethingThatCallsMailService() {
            mailService.sendMail {
            subject Something.EMAIL_SUBJECT // Remove Something prefix
        }
    }

}
+1
source share
1 answer

. DELEGATE_FIRST , Expando . DELEGATE_FIRST EMAIL_SUBJECT , mockMailService, . Expando groovy.lang.MissingPropertyException , null. , ( Something, ).

OWNER_FIRST. DELEGATE_FIRST, mailService sendMail , Expando . .

:

def createMockMailService() {
   def mockMailService = new Object()
   mockMailService.metaClass.sendMail = { callable ->
      callable.delegate = mockMailService
      callable.resolveStrategy = Closure.DELEGATE_FIRST
      callable.call()
   }
   mockMailService.metaClass.subject = { header -> }
   mockMailService
}
+2

All Articles