Skip to content

Properly handle sealed classes with Kotlin 1.7 and JDK 17#916

Merged
Raibaz merged 1 commit intomockk:masterfrom
stuebingerb:fix/fix-sealed-classes
Sep 24, 2022
Merged

Properly handle sealed classes with Kotlin 1.7 and JDK 17#916
Raibaz merged 1 commit intomockk:masterfrom
stuebingerb:fix/fix-sealed-classes

Conversation

@stuebingerb
Copy link
Copy Markdown
Contributor

@stuebingerb stuebingerb commented Sep 6, 2022

Attempt to fix #832 by supporting sealed classes based on previous work done by @aSemy.

@stuebingerb stuebingerb marked this pull request as ready for review September 6, 2022 11:05
@stuebingerb stuebingerb marked this pull request as draft September 6, 2022 14:51
@stuebingerb

This comment was marked as resolved.

@stuebingerb

This comment was marked as outdated.

@aSemy
Copy link
Copy Markdown
Contributor

aSemy commented Sep 6, 2022

Would you be able to add a test using the example that failed? (A sealed class with an abstract child?)

@stuebingerb
Copy link
Copy Markdown
Contributor Author

stuebingerb commented Sep 6, 2022

Would you be able to add a test using the example that failed? (A sealed class with an abstract child?)

Tried, but that test also succeeded without my modifications. I'm still missing something. Not sure if it's actually related to the abstract class.

@stuebingerb

This comment was marked as outdated.

@aSemy
Copy link
Copy Markdown
Contributor

aSemy commented Sep 6, 2022

Yes the stacktrace helps, thanks, although I can't figure out exactly what's wrong. At least I think it shows that we're on the right path! I don't see any reference to ObjenesisInstantiator.kt. The last MockK code called is in io.mockk.proxy.jvm.transformation.SubclassInstrumentation, so something in there (or earlier) isn't handling sealed subclasses properly. I suspect a similar fix will be needed.

@stuebingerb

This comment was marked as outdated.

@stuebingerb
Copy link
Copy Markdown
Contributor Author

stuebingerb commented Sep 7, 2022

Sorry for commit spam. The current implementation here now seems to fix both, mockk's tests and our own. Please kindly review to spot anything I might have missed.

(I've also hidden a few of my now outdated comments)

@stuebingerb stuebingerb marked this pull request as ready for review September 7, 2022 09:48
@stuebingerb
Copy link
Copy Markdown
Contributor Author

stuebingerb commented Sep 8, 2022

So... I tried to add a new test case based on our code and I'm claiming that something in the CI test matrix is currently broken. Have a look at https://github.com/stuebingerb/mockk/pull/10 where the SealedClassTest and SealedInterfaceTest are both running successfully without any adjustments to the implementation.

@aSemy Any idea what might be wrong there?

@aSemy
Copy link
Copy Markdown
Contributor

aSemy commented Sep 8, 2022

That is indeed weird! Good catch. I think the new Gradle GHA is re-using the cache for different JDK versions. I see this message in the logs: Task :modules:mockk:test SKIPPED

@stuebingerb
Copy link
Copy Markdown
Contributor Author

@Raibaz @aSemy What is needed to get this merged?

@aSemy
Copy link
Copy Markdown
Contributor

aSemy commented Sep 23, 2022

I thought that because there was an issue with the Gradle tests not failing #916 (comment) that it wasn't clear if this PR would work as expected.

I don't have any suggestions on how to move forward...

@stuebingerb
Copy link
Copy Markdown
Contributor Author

stuebingerb commented Sep 23, 2022

I guess someone will have to fix the CI. But I currently cannot. Until then I'm claiming that this PR will at least improve things and seems to fix the issues for us.

@aSemy
Copy link
Copy Markdown
Contributor

aSemy commented Sep 24, 2022

Let's do it then 👍

@Raibaz
Copy link
Copy Markdown
Collaborator

Raibaz commented Sep 24, 2022

Ok makes sense, sorry I lost track of what was going on here :)

@DHosseiny
Copy link
Copy Markdown

DHosseiny commented Nov 2, 2022

This change(I guess) breaks mocking a sealed class with kotlin 1.6.21.
In my case the sealed class has abstract class/data class/object childs.

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

Successfully merging this pull request may close these issues.

java.lang.InstantiationError when upgrading to Kotlin 1.7

4 participants