question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Bug: Unable to mock companion field

See original GitHub issue

Prerequisites

Please answer the following questions for yourself before submitting an issue.

  • I am running the latest version
  • I checked the documentation and found no answer
  • I checked to make sure that this issue has not already been filed

Expected Behavior

Able mock variables within companion objects as well as functions

Relates to #136

Context

Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.

  • MockK version: 1.10.01.10.3
  • OS: Windows 10 Home 19041.450
  • Kotlin version: 1.3.721.4.10
  • JDK version: 1114
  • JUnit version: jupiter 5.6.25.7.0
  • Type of test: unit test

Minimal reproducible code (the gist of this issue)

dependencies {
    testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.2'
    testImplementation 'io.mockk:mockk:1.10.0'
}
class Util {

    fun getMessage(): String {
        return message
    }

    companion object {
        val message = "failure"
    }
}
package io.mockk.gh

import io.mockk.every
import io.mockk.mockkObject
import io.mockk.mockkStatic
import org.junit.jupiter.api.Assertions.assertEquals
import org.junit.jupiter.api.Test

class UtilTest {
    @Test
    fun test() {
        val util = Util()
        mockkStatic(Util::class)
        mockkObject(Util)

        every { Util.message } returns "success"

        val result = util.getMessage()

        assertEquals("success", result)
    }
}

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5

github_iconTop GitHub Comments

3reactions
GregHibcommented, Feb 7, 2021

Re-tested with JDK 15.0.2, Kotlin 1.4.30, Mockk 1.10.5 and JUnit 5.7.1, can we get this re-opened @oleksiyp? Thanks in advance.

0reactions
GregHibcommented, Dec 23, 2020

Re-tested with JDK 14, Kotlin 1.4.10, Mockk 1.10.3 and JUnit 5.7.0.

Read more comments on GitHub >

github_iconTop Results From Across the Web

mockkStatic and mockkObject doesn't mock companion ...
According to this answer: https://github.com/mockk/mockk/issues/136#issuecomment-419879755 @Test fun `test class`() { mockkObject(TestClass.
Read more >
Unit testing in Kotlin projects with Mockk vs. Mockito
This post covers how to perform simple unit tests in Kotlin projects using both Mockk and Mockito, as well as covers their differences....
Read more >
Unit Testing verifying a companion object method is called ...
Solution 2: using Mockk Mocking companion object in Mockk is easy. There is no need to insert a test interfacing object in the...
Read more >
Build local unit tests - Android Developers
The Mockable Android library throws an exception if you try to access any of its methods with the Error: "Method ... not mocked...
Read more >
X12N 278 Companion Guide AR2022.10.0 - CMS
Document Number: X12N 278 Companion Guide AR2022.10.0 ... envelope processing status and error codes, two fields, errorCode and errorMessage ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found