Spock Web Console

subscribe to the feed Subscribe
Catch Exception (via #spockwebconsole)
tweet this script Tweet

Catch Exception

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

public class JDrivenServiceSpecification extends Specification {

    private JDrivenService service = new JDrivenService()

    def "publishArticle throws ArticleNotFoundException"() {
        when:
        service.publishArticle null

        then:
        final ArticleNotFoundException exception = thrown()
        // Alternate syntax: def exception = thrown(ArticleNotFoundException)

        exception.message == 'Article not found please provide an article to publish'
    }

}


class JDrivenService {
    void publishArticle(final String article) throws ArticleNotFoundException {
        if (!article) throw new ArticleNotFoundException('Article not found please provide an article to publish')
    }
}

class ArticleNotFoundException extends Exception {
    ArticleNotFoundException(String message) {
        super(message)
    }
}