Spock Web Console

subscribe to the feed Subscribe
Demonstrate Spock Exception Handling (via #spockwebconsole)
tweet this script Tweet

Demonstrate Spock Exception Handling

Published 2 years ago by Warren Smith with tags exception failswith
Actions  ➤ Edit In Console Back To Console Show/Hide Line Numbers View Recent Scripts
import spock.lang.*

class ExceptionHandlerSpec extends Specification {
    
    @FailsWith(NumberFormatException)
    def "Expect a failure using annotation"() {
        expect: Integer.parseInt("99", 8)
      }

    def "Expect a failure"() {
        when: 
        Integer.parseInt("99", 8)
        
        then: 
        NumberFormatException e = thrown()
        e.message == 'For input string: "99"'
      }    
    
    def "Expect a failure not true"() {
        when: 
        Integer.parseInt("-2147483648", 10)

        then: 
        notThrown(NumberFormatException )
      }  
}