Bug: Unable to mock companion field
See original GitHub issuePrerequisites
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:
- Created 3 years ago
- Comments:5
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
Re-tested with JDK 14, Kotlin 1.4.10, Mockk 1.10.3 and JUnit 5.7.0.