@@ -22,11 +22,13 @@ import org.spockframework.runtime.InvalidSpecException
2222import spock.lang.FailsWith
2323import spock.lang.Specification
2424
25+ import org.spockframework.runtime.SpockComparisonFailure
26+
2527/**
2628 *
2729 * @author Peter Niederwieser
2830 */
29- class FailsWithOnMethod extends Specification {
31+ class FailsWithOnMethod extends EmbeddedSpecification {
3032 @FailsWith (IndexOutOfBoundsException )
3133 def ex1 () {
3234 given :
@@ -45,6 +47,41 @@ class FailsWithOnMethod extends Specification {
4547 expect : true
4648 }
4749
50+ @FailsWith (
51+ value = RuntimeException ,
52+ expectedMessage = " My message"
53+ )
54+ def withMessage () {
55+ given :
56+ throw new RuntimeException (" My message" )
57+ }
58+
59+ def " @FailsWith can assert exception message" () {
60+ when :
61+ runner. runSpecBody """
62+ @FailsWith(
63+ value = RuntimeException,
64+ expectedMessage = "My message"
65+ )
66+ def foo() {
67+ given:
68+ throw new RuntimeException("Not my message")
69+ }
70+ """
71+
72+ then :
73+ SpockComparisonFailure e = thrown()
74+ def expected = """ Condition not satisfied:
75+
76+ e.value == expectedMessage
77+ | | |
78+ | | My message
79+ | Not my message
80+ java.lang.RuntimeException: Not my message"""
81+
82+ e. message. startsWith(expected)
83+
84+ }
4885
4986 @FailsWith (ConditionFailedWithExceptionError )
5087 def " can handle ConditionFailedWithExceptionError" () {
@@ -117,4 +154,3 @@ class MySpec extends Specification {
117154 e. message == " @FailsWith needs to refer to an exception type, but does refer to 'java.util.List'"
118155 }
119156}
120-
0 commit comments