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

Fail on mocking sealed subclasses which are final in combination with… #1039

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,12 @@ class ProxyMaker(
instance: Any?
): Cancelable<T> {

throwIfNotPossibleToProxy(clazz, interfaces)

// Sometimes (e.g. in case of sealed classes) we will create the proxy for a subclass of `clazz` and not `clazz`
// itself. We need to determine this early, so that the subclass will be inlined as well.
val actualClass = findActualClassToBeProxied(clazz)

throwIfNotPossibleToProxy(actualClass, interfaces)

val cancellation = inline(actualClass)

val result = CancelableResult<T>(cancelBlock = cancellation)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package io.mockk.it

import io.mockk.MockKException
import io.mockk.every
import io.mockk.mockk
import org.junit.jupiter.api.assertThrows
import kotlin.test.Test
import kotlin.test.assertContains
import kotlin.test.assertEquals
import kotlin.test.fail

class SealedClassMockingTest {

Expand All @@ -15,6 +19,18 @@ class SealedClassMockingTest {
assertEquals(0, base.i)
}

@Test
fun testActualSealedSubClassCannotBeMockedWithMoreInterfacesBecauseItsFinal() {
val exception = assertThrows<MockKException> {
mockk<Base>(moreInterfaces = arrayOf(Runnable::class))
}
exception
.cause
?.message
?.also { assertContains(it, "More interfaces requested and class is final") }
?: fail("Missing cause")
}

private sealed class Base {
abstract val i: Int
}
Expand Down