dpreussler / kotlin-testrunner

JUnit4Testrunner that removes final from classes and methods, especially needed in kotlin projects
113 stars 8 forks source link

Problem when 2 classes are in different packages #18

Open bodiam opened 7 years ago

bodiam commented 7 years ago

I'm trying to mock out a Kotlin service which acts as a collaborator for another service. This is failing, resulting in the following error:

groovy.lang.GroovyRuntimeException: Could not find matching constructor for: address.AddressService(google.GoogleService$$EnhancerByCGLIB$$6fa9e3ff)

    at test.AddressServiceSpec.find address(AddressServiceSpec.groovy:23)

I've created a small project here to reproduce the error: https://github.com/bodiam/kotlin-spock-issue

dpreussler commented 7 years ago

i needed to add also the AddressService to be loaded by kotlintestrunner I assume the default JVM class loader, for some reasons, loaded AddressService first and with it a different version of GoogleService. Sent PR with fix

dpreussler commented 7 years ago

@OpenedClasses([GoogleService, AddressService])

bodiam commented 7 years ago

Hi Danny,

Thanks for checking it out. Interesting, it's not what I expected. I thought that I only needed to specify the class which I want to be opened, and I didn't see the need to open the address service too.

Thanks, Erik

On 18 Dec 2016, at 02:41, Danny Preussler notifications@github.com wrote:

@OpenedClasses([GoogleService, AddressService])

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

dpreussler commented 7 years ago

Your are right, it shouldn't be like this. See our examples. I'll keep this open and will check if the same would happen when writing the test in kotlin. Something is different with yours then with our examples.

For now what you could do is use @OpenedPackages and add your root package there. Might only effect performance a bit.

dpreussler commented 7 years ago

yes it was as I assumed, get more error when doing with kotlin:

java.lang.LinkageError: loader constraint violation: when resolving method "de.jodamob.kotlin.testrunner.anotherpackage.AddressService.(Lde/jodamob/kotlin/testrunner/sample/GoogleService;)V" the class loader (instance of de/jodamob/kotlin/testrunner/NoMoreFinalsClassLoader) of the current class, de/jodamob/kotlin/testrunner/tests/TestWithTwoClasses, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for the method's defining class, de/jodamob/kotlin/testrunner/anotherpackage/AddressService, have different Class objects for the type de/jodamob/kotlin/testrunner/sample/GoogleService used in the signature

and it wont happen if both classes are in same package. Will look more into it

bodiam commented 7 years ago

Interesting, and thanks for the update. It's good to hear that both groovy and Kotlin are having the same, or a similar problem here.

On 19 Dec 2016, at 00:31, Danny Preussler notifications@github.com wrote:

yes it was as I assumed, get more error when doing with kotlin:

java.lang.LinkageError: loader constraint violation: when resolving method "de.jodamob.kotlin.testrunner.anotherpackage.AddressService.(Lde/jodamob/kotlin/testrunner/sample/GoogleService;)V" the class loader (instance of de/jodamob/kotlin/testrunner/NoMoreFinalsClassLoader) of the current class, de/jodamob/kotlin/testrunner/tests/TestWithTwoClasses, and the class loader (instance of sun/misc/Launcher$AppClassLoader) for the method's defining class, de/jodamob/kotlin/testrunner/anotherpackage/AddressService, have different Class objects for the type de/jodamob/kotlin/testrunner/sample/GoogleService used in the signature

and it wont happen if both classes are in same package. Will look more into it

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.