diff --git a/docs/release_notes.adoc b/docs/release_notes.adoc index 0b1aaf2c6d..340fc77b7b 100644 --- a/docs/release_notes.adoc +++ b/docs/release_notes.adoc @@ -20,7 +20,7 @@ include::include.adoc[] * Add new well-known versions to `Jvm` helper to support versions up to 29 spockPull:2057[] ** Built-in extensions have been updated to use this new interface where applicable. * Add best-effort error reporting for interactions on final methods when using the `byte-buddy` mock maker spockIssue:2039[] -* Add support for `@FailsWith` to assert exception message spockIssue:2039[] +* Add support for `@FailsWith` to assert an exception message spockIssue:2039[] * Improve `@Timeout` extension will now use virtual threads if available spockPull:1986[] * Improve mock argument matching, types constraints or arguments in interactions can now handle primitive types like `_ as int` spockIssue:1974[] * Improve `verifyEach` to accept an optional second index parameter for the assertion block closure spockPull:2043[] diff --git a/spock-specs/src/test/groovy/org/spockframework/smoke/extension/FailsWithExtension.groovy b/spock-specs/src/test/groovy/org/spockframework/smoke/extension/FailsWithExtension.groovy index 21c35459e5..f8f2d98c12 100644 --- a/spock-specs/src/test/groovy/org/spockframework/smoke/extension/FailsWithExtension.groovy +++ b/spock-specs/src/test/groovy/org/spockframework/smoke/extension/FailsWithExtension.groovy @@ -22,11 +22,13 @@ import org.spockframework.runtime.InvalidSpecException import spock.lang.FailsWith import spock.lang.Specification +import org.spockframework.runtime.SpockComparisonFailure + /** * * @author Peter Niederwieser */ -class FailsWithOnMethod extends Specification { +class FailsWithOnMethod extends EmbeddedSpecification { @FailsWith(IndexOutOfBoundsException) def ex1() { given: @@ -45,6 +47,41 @@ class FailsWithOnMethod extends Specification { expect: true } + @FailsWith( + value = RuntimeException, + expectedMessage = "My message" + ) + def withMessage() { + given: + throw new RuntimeException("My message") + } + + def "@FailsWith can assert exception message"() { + when: + runner.runSpecBody """ + @FailsWith( + value = RuntimeException, + expectedMessage = "My message" + ) + def foo() { + given: + throw new RuntimeException("Not my message") + } + """ + + then: + SpockComparisonFailure e = thrown() + def expected = """Condition not satisfied: + +e.value == expectedMessage +| | | +| | My message +| Not my message +java.lang.RuntimeException: Not my message""" + + e.message.startsWith(expected) + + } @FailsWith(ConditionFailedWithExceptionError) def "can handle ConditionFailedWithExceptionError"() { @@ -117,4 +154,3 @@ class MySpec extends Specification { e.message == "@FailsWith needs to refer to an exception type, but does refer to 'java.util.List'" } } -