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

Bug: mocking java static method exception - o.mockk.MockKException: No other calls allowed in stdObjectAnswer than equals/hashCode/toString #49

Closed
pavelaizen opened this issue Mar 21, 2018 · 5 comments
Labels

Comments

@pavelaizen
Copy link

pavelaizen commented Mar 21, 2018

sample

running that code leads to MockKException:

objc[29348]: Class JavaLaunchHelper is implemented in both /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/bin/java (0x1042c24c0) and /Library/Java/JavaVirtualMachines/jdk1.8.0_144.jdk/Contents/Home/jre/lib/libinstrument.dylib (0x10434e4e0). One of the two will be used. Which one is undefined.
2018-03-21 12:17:18 DEBUG - [AbstractMockFactory] Creating spyk for MyLog4jExecutor name=#1, moreInterfaces=[]
2018-03-21 12:17:18 DEBUG - [AbstractMockFactory] Creating mockk for Thread name=#2, moreInterfaces=[]
2018-03-21 12:17:19 DEBUG - [AbstractMockFactory] Creating mockk for Runnable name=#3, moreInterfaces=[]
2018-03-21 12:17:19 DEBUG - [JvmStaticMockFactory] Creating static mockk for ThreadContext
2018-03-21 12:17:19 DEBUG - [MockKProxyMaker] Injecting handler to class org.apache.logging.log4j.ThreadContext for static methods

io.mockk.MockKException: No other calls allowed in stdObjectAnswer than equals/hashCode/toString

at io.mockk.impl.stub.MockKStub.stdObjectAnswer(MockKStub.kt:78)

part of my dependencies:
dependencies

@oleksiyp
Copy link
Collaborator

Not able to reproduce with following code:

import io.mockk.*
import org.apache.logging.log4j.ThreadContext
import kotlin.test.Test

class Issue49Test {
    class MyLog4jExecutor {
        fun runBefore(t: Thread?, r: Runnable?) = ThreadContext.putAll(mutableMapOf("a" to "b"))
    }

    @Test
    fun beforeExecute() {
        val executor = spyk<MyLog4jExecutor>(recordPrivateCalls = true)
        val t = mockk<Thread>(relaxed = true)
        val r = mockk<Runnable>(relaxed = true)
        staticMockk(ThreadContext::class.java.name).use {
            executor.runBefore(t,r)
            verify { ThreadContext.putAll(any()) }
        }

    }
}

@oleksiyp
Copy link
Collaborator

Need definition of Log4jExecutor

@oleksiyp oleksiyp added the bug label Mar 21, 2018
@pavelaizen
Copy link
Author

pavelaizen commented Mar 26, 2018

@oleksiyp oleksiyp changed the title mocking java static method exception - o.mockk.MockKException: No other calls allowed in stdObjectAnswer than equals/hashCode/toString Bug: mocking java static method exception - o.mockk.MockKException: No other calls allowed in stdObjectAnswer than equals/hashCode/toString May 16, 2018
@nieldw
Copy link

nieldw commented Nov 4, 2018

I encounter the same issue with this code on version 1.8.9

import org.apache.logging.log4j.LogManager
import org.apache.logging.log4j.Logger
import org.junit.jupiter.api.Test

class LoggingTest {
 @Test
    fun `Something should be logged`() {
        mockkStatic(LogManager::class) {
            val logger = mockk<Logger>()
            every { LogManager.getLogger(any() as String) } returns logger
            // check stuff
        }
    }
}

@oleksiyp oleksiyp added this to To do in MockK features Dec 25, 2018
@oleksiyp
Copy link
Collaborator

oleksiyp commented Feb 13, 2019

@nieldw this is not totally same issue. Please open a new ticket. I reproduced only that log4j.Logget is not mockable, but this doesnt match with your statement. Closing original issue as it seems this part of code significantly changed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
MockK features
  
To do
Development

No branches or pull requests

3 participants