0

I have a Grails service class that I'm trying to write a Spock test for. The signature of the method is as follows:

def buildErrorJsonArray(AddressInfoCommand addressInfoCmd, PaymentInfoCommand paymentInfoCmd, boolean trsRequest = false) 

When I populate the AddressInfoCommand and PaymentInfoCommand command object with invalid data in the test and call validate on it, it's not returning any errors and I'm not sure why. My guess is it's the way I'm mocking the command objects in the test (via mockForConstraintsTests). Here is the part of the test that populates the PaymentInfoCommand:

setup:
    service.messageSource = [getMessage: { errors, locale -> return "message" }]
    mockForConstraintsTests(AddressInfoCommand)
    mockForConstraintsTests(PaymentInfoCommand)

    PaymentInfoCommand paymentInfoCommand = new PaymentInfoCommand()
    def payment = new Payment()
    payment.paymentType = ""
    payment.cardAccountNumber = ""
    payment.cardExpirationDate = ""
    payment.cardSecurityCode = ""
    paymentInfoCommand.setPaymentInfoCommand(payment)
    paymentInfoCommand.validate() 

And here is part of the PaymentInfoCommand:

class PaymentInfoCommand {
    String cardType = ""
    String cardAccountNumber = ""
    String cardExpirationDateYear = ""
    String cardExpirationDateMonth = ""
    String cardSecurityCode = ""
    def messageSource

    static constraints = {
        cardAccountNumber(nullable: false, blank: false, maxSize: 19, creditCard: true)

        cardSecurityCode(nullable: false, blank: false, minSize: 3, maxSize: 4, validator: {val, obj ->
            if (!StringUtils.isNumeric(obj.cardSecurityCode)) {
                return "paymentInfoCommand.cardSecurityCode.notANumber.error"
            }
        })
    } }

Can anyone see what I'm doing wrong?

Alidad
  • 5,463
  • 1
  • 24
  • 47
user1866924
  • 431
  • 5
  • 17
  • What version of Grails you are using? might be this issue discussed [here](http://stackoverflow.com/questions/16108556/grails-2-1-unit-testing-command-object-mockforconstraintstests-not-working) – Alidad May 29 '14 at 21:14
  • I'm using version 2.2.3 – user1866924 May 29 '14 at 21:16
  • Per the same discussion on [here](http://stackoverflow.com/questions/16108556/grails-2-1-unit-testing-command-object-mockforconstraintstests-not-working) and [this bug](https://jira.grails.org/browse/GRAILS-9162), if you remove the @Validateable annotation and add grails.validateable.classes = [packagename.PaymentInfoCommand] in your config.groovy, it should work. – Alidad May 29 '14 at 21:58
  • I tried your suggestion and removed the @Validateable annotation and added the grails.validateable.classes = [packagename.PaymentInfoCommand] to Config.groovy but still no luck. – user1866924 May 30 '14 at 15:46

0 Answers0