How to use Spock library
First of all, Spock is a unit-testing library which is usually used with Grails and Groovy. So you have to import the spock dependency in the Build.groovy or Pom.xml:
‘org.spockframework:spock-core:1.0-groovy-2.4’
group: ‘cglib’, name: ‘cglib-nodep’, version: ‘2.2.2’
(* Cglib library, it is necessary for mocking objects)
How to catch Exceptions with Spock
With Spock you can check everything you think, but in this article we are going to focus on Exception and its management.
In addition, you can catch an exception as follows:
1 2 3 4 5 6 7 8 9 10 11 12 |
void "paymentService throws ServiceException if paymentId is null"(){ given: "An external payment service" PaymentService paymentService = new PaymentService() PaymentExternalService paymentExternalService = Mock(PaymentExternalService) and:"it receives a paymentId null" Long paymentIdNull = null paymentExternalService.retrievePaymentInfo(paymentIdNull) >> {throw new Exception()} when: paymentService.retrievePaymentInfo(paymentIdNull) then: thrown ExternalServiceException } |
In the test code you can see, the external service has been mocked in order to throw a NullPointerException, so our Payment service catch it and it will throw the final ExternalServiceException, as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
package test.service class PaymentService { PaymentExternalService paymentExternalService List retrievePaymentInfo(Long paymentId) throws ExternalServiceException{ List paymentsInfo = [] try{ paymentsInfo = paymentExternalService.retrievePaymentInfo(paymentExternalService) }catch (Exception exception){ throw new ExternalServiceException("External Payment Error","PaymentService","Payment") } paymentsInfo } } |
It is very common, the customer exception thrown has some attributes, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
package test.service class ExternalServiceException extends Exception { private String operation private String service ExternalServiceException (String message, String operationAfected, String serviceThrownError){ super(message) operation = operationAfected service = serviceThrownError } String getOperation() { operation } String getService() { service } } |
How to check Exception attributes’
Threfore, you would be able to check all of the attributes which the Exception thrown had, as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void "paymentService throws ServiceException whose operation is PaymentService and Date is the current moment if paymentId is null"(){ given: "An external payment service" PaymentService paymentService = new PaymentService() PaymentExternalService paymentExternalService = Mock(PaymentExternalService) and:"it receives a paymentId null" Long paymentIdNull = null paymentExternalService.retrievePaymentInfo(paymentIdNull) >> {throw new Exception()} when: paymentService.retrievePaymentInfo(paymentIdNull) then: final ExternalServiceException externalPaymentServiceException = thrown() "PaymentService" == externalPaymentServiceException.getOperation() "Payment" == externalPaymentServiceException.getService() } |
You can read other interesting articles like Defensive Programming and Resilient Systems. Also you can visit another interesting blogs like Mrhaki’s blog.
Best Regards