vendredi 19 décembre 2014

How to test non controller / domain / service classes in Grails ..

I have a Groovy helper class defined in sources/groovy which i'm trying to test using Spock .. The class i'm trying to test has a reference to org.springframework.validation.Errors called errors ..


I have a test defined which invokes some code shown below ..



def validateSuppliedDocument() {

// If we create one it's always valid ..

if (txn.transactionType.autoCreateDocument) {
return true
} else {

txn.documentHeader =
DocumentHeader.findByDocumentReferenceCodeAndDocumentType(txn.documentReferenceCode, txn.transactionType.associatedDocumentType)

if (!txn.documentHeader) {
errors.rejectValue('documentReferenceCode', "document.not.found",
[txn.documentReferenceCode].toArray(), 'Document not found')
return false
}


My problem is caused by the 'errors.rejectValue' statement as the errors instance is null ..


How can I get grails to inject this just for testing purposes and what class should it be given that Errors is an interface ??


just for info this method gets called as part of validator on a field (if that helps ..) and works fine in production ..


Thanks


Aucun commentaire:

Enregistrer un commentaire