Skip to content

Commit

Permalink
Add tests for @FailsWith error message
Browse files Browse the repository at this point in the history
  • Loading branch information
leonard84 committed Dec 28, 2024
1 parent e081141 commit 6d9e671
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
2 changes: 1 addition & 1 deletion docs/release_notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,24 @@ 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:
def foo = []
foo.get(0)
}

@FailsWith(Exception)
@FailsWith(
value = Exception,
expectedMessage = "Index 0 out of bounds for length 0"
)
def ex2() {
given:
def foo = []
Expand All @@ -45,6 +50,33 @@ class FailsWithOnMethod extends Specification {
expect: true
}

def "@FailsWith can assert exception message"() {
when:
runner.runSpecBody """
@FailsWith(
value = Exception,
expectedMessage = "Index 1 out of bounds for length 1"
)
def foo() {
given:
def foo = []
foo.get(0)
}
"""

then:
SpockComparisonFailure e = thrown()
def expected = """Condition not satisfied:
e.value == expectedMessage
| | |
| | Index 1 out of bounds for length 1
| Index 0 out of bounds for length 0
java.lang.IndexOutOfBoundsException: Index 0 out of bounds for length 0"""

e.message.startsWith(expected)

}

@FailsWith(ConditionFailedWithExceptionError)
def "can handle ConditionFailedWithExceptionError"() {
Expand Down Expand Up @@ -117,4 +149,3 @@ class MySpec extends Specification {
e.message == "@FailsWith needs to refer to an exception type, but does refer to 'java.util.List'"
}
}

0 comments on commit 6d9e671

Please sign in to comment.