Skip to content

Commit b6b32d0

Browse files
committed
Fixup exception handling on MissingPropertyException
1 parent ec29d9c commit b6b32d0

File tree

3 files changed

+45
-2
lines changed

3 files changed

+45
-2
lines changed

spock-core/src/main/java/org/spockframework/mock/runtime/GroovyRealGetPropertyInvoker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public Object respond(IMockInvocation invocation) {
5353
Object receiver = invocation.getMockObject().getInstance();
5454
try {
5555
return metaClass.getProperty(sender, receiver, property, useSuper, fromInsideClass);
56-
} catch (InvokerInvocationException | MissingMethodException e2) {
56+
} catch (InvokerInvocationException | MissingPropertyException e2) {
5757
e2.addSuppressed(e1);
5858
throw e2;
5959
}

spock-core/src/main/java/org/spockframework/mock/runtime/GroovyRealSetPropertyInvoker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public Object respond(IMockInvocation invocation) {
5555
try {
5656
metaClass.setProperty(sender, receiver, property, newValue, useSuper, fromInsideClass);
5757
return null;
58-
} catch (InvokerInvocationException | MissingMethodException e2) {
58+
} catch (InvokerInvocationException | MissingPropertyException e2) {
5959
e2.addSuppressed(e1);
6060
throw e2;
6161
}

spock-specs/src/test/groovy/org/spockframework/smoke/mock/GroovySpiesThatAreGlobal.groovy

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,49 @@ class GroovySpiesThatAreGlobal extends Specification {
247247
_ | { PersonWithOverloadedMethods.perform((Pattern) null) }
248248
}
249249

250+
def "Accessing non-existing property throws MissingPropertyException"() {
251+
when:
252+
Enclosing.Super.NON_EXISTING_FIELD
253+
then:
254+
def ex = thrown(MissingPropertyException)
255+
ex.message.contains("NON_EXISTING_FIELD")
256+
}
257+
258+
def "Accessing non-existing property through global spy throws MissingPropertyException"() {
259+
given:
260+
GroovySpy(Enclosing.Super, global: true)
261+
262+
when:
263+
Enclosing.Super.NON_EXISTING_FIELD
264+
then:
265+
def ex = thrown(MissingPropertyException)
266+
ex.message.contains(" NON_EXISTING_FIELD ")
267+
def suppressed = ex.getSuppressed()[0].cause
268+
suppressed instanceof MissingMethodException
269+
suppressed.message.contains("getNON_EXISTING_FIELD")
270+
}
271+
272+
def "Accessing non-existing property setter throws MissingPropertyException"() {
273+
when:
274+
Enclosing.Super.NON_EXISTING_FIELD = ""
275+
then:
276+
thrown(MissingPropertyException)
277+
}
278+
279+
def "Accessing non-existing property setter through global spy throws MissingPropertyException"() {
280+
given:
281+
GroovySpy(Enclosing.Super, global: true)
282+
283+
when:
284+
Enclosing.Super.NON_EXISTING_FIELD = ""
285+
then:
286+
def ex = thrown(MissingPropertyException)
287+
ex.message.contains(" NON_EXISTING_FIELD ")
288+
def suppressed = ex.getSuppressed()[0].cause
289+
suppressed instanceof MissingMethodException
290+
suppressed.message.contains("setNON_EXISTING_FIELD")
291+
}
292+
250293
def "Accessing static field through global spy shall only log single invocation"() {
251294
given:
252295
GroovySpy(Enclosing.Super, global: true)

0 commit comments

Comments
 (0)