Skip to content

Commit 2499dbd

Browse files
committed
Add tests for @FailsWith error message
1 parent e081141 commit 2499dbd

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

docs/release_notes.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ include::include.adoc[]
2020
* Add new well-known versions to `Jvm` helper to support versions up to 29 spockPull:2057[]
2121
** Built-in extensions have been updated to use this new interface where applicable.
2222
* Add best-effort error reporting for interactions on final methods when using the `byte-buddy` mock maker spockIssue:2039[]
23-
* Add support for `@FailsWith` to assert exception message spockIssue:2039[]
23+
* Add support for `@FailsWith` to assert an exception message spockIssue:2039[]
2424
* Improve `@Timeout` extension will now use virtual threads if available spockPull:1986[]
2525
* Improve mock argument matching, types constraints or arguments in interactions can now handle primitive types like `_ as int` spockIssue:1974[]
2626
* Improve `verifyEach` to accept an optional second index parameter for the assertion block closure spockPull:2043[]

spock-specs/src/test/groovy/org/spockframework/smoke/extension/FailsWithExtension.groovy

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ import org.spockframework.runtime.InvalidSpecException
2222
import spock.lang.FailsWith
2323
import 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

Comments
 (0)