Skip to content

Wrong stubbed exception type with @SpringBean #887

Closed
@szpak

Description

Issue description

Stubbed exception thrown by a stub/mock injected into Spring context is wrapped with InvocationTargetException when called through another (real) Spring bean (1.2-RC1).

How to reproduce

@ContextConfiguration(classes = [Parent, Slave])
class SpringBeanExceptionBugITSpec extends Specification {

    @Autowired
    private Parent parent

    @SpringBean
    private Slave slaveMock = Stub()    //the same with Mock()

    @Ignore("Broken. Catches InvocationTargetException which wraps UnsupportedOperationException")
    def "should catch stubbed exception"() {
        given:
            slaveMock.throwException() >> { throw new UnsupportedOperationException("Stubbed test exception") }
        when:
            parent.throwExceptionInSlave()
//            slaveMock.throwException()    //it works properly
        then:
            UnsupportedOperationException e = thrown() //assertion error
            e.message == "Stubbed test exception"
    }
}

@Component
class Parent {
    @Autowired
    Slave slave

    void throwExceptionInSlave() {
        slave.throwException()
    }
}

class Slave {
    void throwException() {
        throw new RuntimeException("Test exception")
    }
}

Results in:

Expected exception of type 'java.lang.UnsupportedOperationException', but got 'java.lang.reflect.InvocationTargetException'

	at org.spockframework.lang.SpecInternals.checkExceptionThrown(SpecInternals.java:80)
	at org.spockframework.lang.SpecInternals.thrownImpl(SpecInternals.java:63)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at info.solidsoft.blog.spock12.spring.SpringBeanExceptionBugITSpec.should catch stubbed exception(SpringBeanExceptionBugITSpec.groovy:27)
Caused by: java.lang.reflect.InvocationTargetException
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at org.spockframework.spring.mock.DelegatingInterceptor.intercept(DelegatingInterceptor.java:53)
	at org.spockframework.mock.runtime.ByteBuddyInterceptorAdapter.interceptNonAbstract(ByteBuddyInterceptorAdapter.java:35)
	at info.solidsoft.blog.spock12.spring.Parent.throwExceptionInSlave(SpringBeanExceptionBugITSpec.groovy:38)
	at info.solidsoft.blog.spock12.spring.SpringBeanExceptionBugITSpec.should catch stubbed exception(SpringBeanExceptionBugITSpec.groovy:24)
Caused by: java.lang.UnsupportedOperationException: Stubbed test exception
	at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
	at info.solidsoft.blog.spock12.spring.SpringBeanExceptionBugITSpec.should catch stubbed exception_closure1(SpringBeanExceptionBugITSpec.groovy:22)
	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
	at groovy.lang.Closure.call(Closure.java:421)
	at org.spockframework.mock.response.CodeResponseGenerator.invokeClosure(CodeResponseGenerator.java:54)
	at org.spockframework.mock.response.CodeResponseGenerator.doRespond(CodeResponseGenerator.java:37)
	at org.spockframework.mock.response.SingleResponseGenerator.respond(SingleResponseGenerator.java:32)
	at org.spockframework.mock.response.ResponseGeneratorChain.respond(ResponseGeneratorChain.java:44)
	at org.spockframework.mock.runtime.MockInteraction.accept(MockInteraction.java:72)
	at org.spockframework.mock.runtime.MockInteractionDecorator.accept(MockInteractionDecorator.java:50)
	at org.spockframework.mock.runtime.InteractionScope$1.accept(InteractionScope.java:51)
	at org.spockframework.mock.runtime.MockController.handle(MockController.java:40)
	at org.spockframework.mock.runtime.JavaMockInterceptor.intercept(JavaMockInterceptor.java:72)
	at org.spockframework.mock.runtime.ByteBuddyInterceptorAdapter.interceptNonAbstract(ByteBuddyInterceptorAdapter.java:35)
	... 6 more

Additional Environment information

Java 9.0.4, Groovy 2.5.2, Idea, Linux

Build-tool dependencies used

spock-core:1.2-RC1-groovy-2.5

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions