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

Broken mocking of sealed classes after upgrading to 1.13.2 #1015

Open
vudzkostek opened this issue Jan 2, 2023 · 7 comments
Open

Broken mocking of sealed classes after upgrading to 1.13.2 #1015

vudzkostek opened this issue Jan 2, 2023 · 7 comments

Comments

@vudzkostek
Copy link

vudzkostek commented Jan 2, 2023

Prerequisites

After upgrading mockk dependency from 1.13.1 to 1.13.2 many tests started to fail in our project. It seems to be related to #916.

The problem is related to sealed classes or interfaces with child objects or data classes.

To make it easier to understand, I've prepared reproduction project:
https://github.com/vudzkostek/MockKReproduce

There are two different crashes in my sample project, both related to version 1.13.2. When downgraded to 1.13.1 everything passes.

I am not entirely sure if these failing tests are intended result of #916 or real issue that should be fixed in next release.

Stack traces

Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
io.mockk.MockKException: Missing mocked calls inside every { ... } block: make sure the object inside the block is a mock
java.lang.NullPointerException
	at com.kosta.mockkreproduction.OtherSealedClass$Subtype1.hashCode(OtherSealedClass.kt)
@cdprete
Copy link

cdprete commented Jan 4, 2023

1.13.3 has the same issue (at least, with sealed interfaces)...

@vudzkostek
Copy link
Author

@stuebingerb is behaviour described in this issue is expected result of #916 ?

@stuebingerb
Copy link
Contributor

stuebingerb commented Jan 5, 2023

@stuebingerb is behaviour described in this issue is expected result of #916 ?

I would have to look into it but as a first response I think it is safe to assume that this is a result of my lacking knowledge of mockk as well as the issues I had with actually testing my changes, i.e. unexpected. And IMHO running code should not break with a new bugfix version.

@cdprete
Copy link

cdprete commented Jan 23, 2023

A small example:

import io.mockk.impl.annotations.RelaxedMockK
import io.mockk.junit5.MockKExtension
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.extension.ExtendWith

/**
 * @author tk35c
 * @since 23/01/2023
 **/
@ExtendWith(MockKExtension::class)
class TestBrokenMockSealedInterface {
    @RelaxedMockK
    private lateinit var myInterface: MyInterface

    @Test
    fun `should not print`() {
        myInterface.foo()
    }
}

sealed interface MyInterface {
    fun foo()
}
class MyClass: MyInterface {
    override fun foo() {
        error("Should not come here")
    }
}

The real method of MyClass gets called when, instead, it should be mocked ideally with a NOOP.

@kkurczewski
Copy link
Contributor

Seems to be fixed in 1.13.3

@snijsure
Copy link

I still see this issue 1.13.5

@lecogiteur
Copy link

Same issue in 1.13.3 and 1.13.8 it's seems to works on mac but not on linux (we use same jvm version). We try with openjdk and oracle jdk (17.0.0)

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

6 participants