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

Issue with Mocks for Kotlin Classes #25

Open
srikumarcs opened this issue Apr 4, 2019 · 2 comments
Open

Issue with Mocks for Kotlin Classes #25

srikumarcs opened this issue Apr 4, 2019 · 2 comments

Comments

@srikumarcs
Copy link

Hello,

I am facing a very interesting question when using Spock and this extension with Kotlin classes. If I use the @Collaborator annotation in my spec to mock a collaborator & stub one of the response of the said collaborator, I can see that the stubbed method never gets used. The framework makes a call to a real instance instead of the mocks. If I convert my collaborator to Java, it works as expected. Allow me to explain the issue with a code snippet

class CancelItemBuilder(private val paymentMethodBuilder: PaymentMethodBuilder, private val changeLog: ChangeFactory) {
    fun display(): String {
        return description + paymentMethodBuilder.display() + changeLog.display()
    }
    companion object {
        internal var description = "CancelItemBuilder"
    }

}

open class PaymentMethodBuilder(internal var clockIntance: ClockIntance) {
   fun display(): String {
        return description + clockIntance.display()
    }
    companion object {
        internal var description = "PaymentMethodBuilder"
    }
}

open class ChangeFactory {
    fun display(): String {
        return description
    }
    companion object {
        internal var description = "ChangeFactory"
    }
}

And here is the test

class CancelItemBuilderSpec extends Specification {
    @Subject CancelItemBuilder cancelItemBuilder
    @Collaborator PaymentMethodBuilder paymentMethodBuilder  = Mock()
    @Collaborator ChangeFactory changeLog = Mock()

    def "test this concept"(){
        given:
        paymentMethodBuilder.display() >>"Mocked PaymentMethodBuilder"
        changeLog.display() >>"Mocked ChangeFactory"

        when:
        def result = cancelItemBuilder.display()

        then:
        result == "CancelItemBuilder Mocked PaymentMethodBuilder Mocked ChangeFactory"
    }

}

And here is the result of the test, which throw a NPE coz the real instance gets called instead of the mocks!

java.lang.NullPointerException
	at com.johnlewis.order.manage.PaymentMethodBuilder.display(PaymentMethodBuilder.kt:6)
	at com.johnlewis.order.manage.CancelItemBuilder.display(CancelItemBuilder.kt:6)
	at com.johnlewis.order.manage.CancelItemBuilderSpec.test this crap(CancelItemBuilder.groovy:18)
@leonard84
Copy link

@srikumarcs does it work if you construct it manually in Spock?

@IgorKomissarov
Copy link

Got the same issue with manually creating mock in Spock.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants