Skip to content

Commit

Permalink
Make giving null to thrown() consistent with mock object creation (#1799
Browse files Browse the repository at this point in the history
)

Co-authored-by: Leonard Brünings <[email protected]>
  • Loading branch information
Vampire and leonard84 authored Nov 9, 2023
1 parent 66fe00d commit 2c0731d
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,26 +64,25 @@ <T> T oldImpl(T expression) {
}

<T extends Throwable> T thrownImpl(String inferredName, Class<T> inferredType) {
if (inferredType == null) {
throw new InvalidSpecException("Thrown exception type cannot be inferred automatically. " +
"Please specify a type explicitly (e.g. 'thrown(MyException)').");
}

return inferredType.cast(checkExceptionThrown(inferredType));
}

Throwable thrownImpl(String inferredName, Class<? extends Throwable> inferredType, Class<? extends Throwable> specifiedType) {
return checkExceptionThrown(specifiedType);
return checkExceptionThrown(specifiedType == null ? inferredType : specifiedType);
}

Throwable checkExceptionThrown(Class<? extends Throwable> exceptionType) {
Throwable actual = specificationContext.getThrownException();
if (exceptionType == null) {
throw new InvalidSpecException("Thrown exception type cannot be inferred automatically. " +
"Please specify a type explicitly (e.g. 'thrown(MyException)').");
}

if (!Throwable.class.isAssignableFrom(exceptionType))
throw new InvalidSpecException(
"Invalid exception condition: '%s' is not a (subclass of) java.lang.Throwable"
).withArgs(exceptionType.getSimpleName());

Throwable actual = specificationContext.getThrownException();
if (exceptionType.isInstance(actual)) return actual;

throw new WrongExceptionThrownError(exceptionType, actual);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,51 @@ thrown() == e
e.message.startsWith("Exception conditions are only allowed as top-level statements")
}

def "thrown condition with undeterminable exception type"() {
when:
runner.runFeatureBody """
when:
true
then:
$thrownInteraction
"""

then:
InvalidSpecException e = thrown()
e.message == "Thrown exception type cannot be inferred automatically. Please specify a type explicitly (e.g. 'thrown(MyException)')."

where:
thrownInteraction << [
'thrown()',
'thrown(null)',
'def e = thrown(null)'
]
}

def "thrown condition with explicit type null and inferred type"() {
when:
throw new Exception()

then:
Exception e = thrown(null)
}

def "thrown condition with explicit and inferred type"() {
when:
runner.runFeatureBody """
when:
throw new Exception()
then:
Exception e = thrown(IOException)
"""

then:
WrongExceptionThrownError e = thrown()
e.message == "Expected exception of type 'java.io.IOException', but got 'java.lang.Exception'"
}

@Issue("https://github.com/spockframework/spock/issues/260")
@FailsWith(InvalidSpecException)
def "(Java-style) exception condition must specify a type that is-a java.lang.Throwable"() {
Expand Down

0 comments on commit 2c0731d

Please sign in to comment.