From 2c0731d923992ebb32be01e59ace85c39457cf88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Kautler?= Date: Thu, 9 Nov 2023 13:02:23 +0100 Subject: [PATCH] Make giving null to thrown() consistent with mock object creation (#1799) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Leonard Brünings --- .../spockframework/lang/SpecInternals.java | 13 +++--- .../condition/ExceptionConditions.groovy | 45 +++++++++++++++++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/spock-core/src/main/java/org/spockframework/lang/SpecInternals.java b/spock-core/src/main/java/org/spockframework/lang/SpecInternals.java index 61d5366797..cf42559df1 100644 --- a/spock-core/src/main/java/org/spockframework/lang/SpecInternals.java +++ b/spock-core/src/main/java/org/spockframework/lang/SpecInternals.java @@ -64,26 +64,25 @@ T oldImpl(T expression) { } T thrownImpl(String inferredName, Class 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 inferredType, Class specifiedType) { - return checkExceptionThrown(specifiedType); + return checkExceptionThrown(specifiedType == null ? inferredType : specifiedType); } Throwable checkExceptionThrown(Class 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); diff --git a/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ExceptionConditions.groovy b/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ExceptionConditions.groovy index 28ebdf9b8f..49b977ade3 100644 --- a/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ExceptionConditions.groovy +++ b/spock-specs/src/test/groovy/org/spockframework/smoke/condition/ExceptionConditions.groovy @@ -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"() {