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?