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

Mock Kotlin的 companion object 方法异常 #312

Open
LitterSun opened this issue Jan 6, 2023 · 0 comments
Open

Mock Kotlin的 companion object 方法异常 #312

LitterSun opened this issue Jan 6, 2023 · 0 comments

Comments

@LitterSun
Copy link

OneUtil.kt

class OneUtil {
    fun add(x: Int, y: Int): Int {
        return staticAdd(x, y)
    }

    companion object {
        fun staticAdd(x: Int, y: Int): Int {
            return x + y
        }
    }
}

OneUtilTest.kt

class OneUtilTest {
    class Mock {
        @MockInvoke(targetClass = OneUtil::class)
        private fun staticAdd(x: Int, y: Int): Int {
            return 100
        }
    }
    
    @Test
    fun exampleTest() {
        assertEquals(100, OneUtil().add(1, 3))
    }
}

执行后出错

Bad type on operand stack
Exception Details:
  Location:
    com/example/xxx/OneUtil.add(II)I @8: invokevirtual
  Reason:
    Type 'com/example/xxx/OneUtil$Companion' (current frame, stack[2]) is not assignable to integer
  Current Frame:
    bci: @8
    flags: { }
    locals: { 'com/example/xxx/OneUtil', integer, integer }
    stack: { 'com/example/xxx/OneUtilTest$Mock', null, 'com/example/xxx/OneUtil$Companion', integer }
  Bytecode:
    0000000: b800 2601 b200 281b b600 2cac          

java.lang.VerifyError: Bad type on operand stack
Exception Details:
  Location:
    com/example/xxx/OneUtil.add(II)I @8: invokevirtual
  Reason:
    Type 'com/example/xxx/OneUtil$Companion' (current frame, stack[2]) is not assignable to integer
  Current Frame:
    bci: @8
    flags: { }
    locals: { 'com/example/xxx/OneUtil', integer, integer }
    stack: { 'com/example/xxx/OneUtilTest$Mock', null, 'com/example/xxx/OneUtil$Companion', integer }
  Bytecode:
    0000000: b800 2601 b200 281b b600 2cac   
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

1 participant