Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to spy existing object if it does not have non argument contructor #769

Closed
szpak opened this issue Sep 13, 2017 · 2 comments

Comments

@szpak
Copy link
Contributor

commented Sep 13, 2017

Issue description

Spock fails to create a spy on a real object if it doesn't have a non argument constructor.

org.spockframework.mock.CannotCreateMockException: Cannot create mock for class info.solidsoft.gradle.cdeliveryboy.FooSpec$Foonull

	at org.spockframework.mock.runtime.MockInstantiator.instantiate(MockInstantiator.java:38)
	at org.spockframework.mock.runtime.ProxyBasedMockFactory$ByteBuddyMockFactory.createMock(ProxyBasedMockFactory.java:133)
	at org.spockframework.mock.runtime.ProxyBasedMockFactory.create(ProxyBasedMockFactory.java:65)
	at org.spockframework.mock.runtime.JavaMockFactory.createInternal(JavaMockFactory.java:59)
	at org.spockframework.mock.runtime.JavaMockFactory.create(JavaMockFactory.java:40)
	at org.spockframework.mock.runtime.CompositeMockFactory.create(CompositeMockFactory.java:44)
	at org.spockframework.lang.SpecInternals.createMock(SpecInternals.java:51)
	at org.spockframework.lang.SpecInternals.createMockImpl(SpecInternals.java:296)
	at org.spockframework.lang.SpecInternals.SpyImpl(SpecInternals.java:185)
	at info.solidsoft.gradle.cdeliveryboy.FooSpec.should create spy for real object which does not have non argument constructor(FooSpec.groovy:21)
Caused by: groovy.lang.GroovyRuntimeException: Could not find matching constructor for: info.solidsoft.gradle.cdeliveryboy.FooSpec$Foo$SpockMock$astcHGKS()
	at org.spockframework.mock.runtime.MockInstantiator.instantiate(MockInstantiator.java:33)
	... 9 more

Spock seems to not use Objenesis to create an instance of the wrapping object. If manually switched to useObjenesis (in debugger as Spy(obj) does not provide that variant) it works fine. However, for spies probably a constructor should not be called in any case (while working with real objects).

How to reproduce

    def "should create spy for real object which does not have non argument constructor"() {
        expect:
            Spy(new Foo(4))
    }

    static class Foo {
        Foo(int v) {
        }
    }

Additional Environment information

Spock 1.1-groovy-2.4, Byte Buddy 1.7.5, Objenesis 2.6, Groovy 2.4.10, Java 1.8.0_144

@leonard84

This comment has been minimized.

Copy link
Member

commented Sep 13, 2017

However, for spies probably a constructor should not be called in any case (while working with real objects).

To clarify, spies need to be instantiated using their constructor otherwise the internal state is compromised. For the newly added feature of spying on existing instances this was already done beforehand (by the user or a framework), so the usage of useObjenesis is OK here.

@szpak

This comment has been minimized.

Copy link
Contributor Author

commented Sep 14, 2017

That's while I precised "while working with real objects" as th end of my utterance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.