Can not mock super method call in mockK?
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
super.print()
is mocked by answers
.
Current Behavior
super.print()
is not mocked.
Context
- MockK version: 1.11.0
- OS: Mac OsX
- Kotlin version: 1.3
- JDK version: Java 8
- JUnit version: 4.13.1
- Type of test: unit test
Minimal reproducible code (the gist of this issue)
class SuperCallTest {
open class Parent {
open fun print() {
println("Parent")
}
}
class Children: Parent() {
override fun print() {
super.print()
println("Children")
}
}
@Test
fun testCallSuper() {
val children = Children()
val spy = spyk(children)
every { (spy as Parent).print() } answers { nothing }
children.print()
}
@Test
fun testCallSuper2() {
val children = Children()
val spy = spyk(children as Parent)
every { spy.print() } answers { nothing }
children.print()
}
@Test
fun testCallSuper3() {
mockkConstructor(Parent::class)
every { anyConstructed<Parent>().print() } answers { nothing }
val children = Children()
children.print()
}
}
Output
Console output:
Parent
Children
for both 3 cases.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:5
Top Results From Across the Web
How to mock super method by MockK - Stack Overflow
I want to use MockK to verify if onCreate() is invoked, then doSomething() is invoked too. However I don't know how to mock...
Read more >MockK | mocking library for Kotlin
Provides DSL to mock behavior. Built from zero to fit Kotlin language. Supports named parameters, object mocks, coroutines and extension function mocking.
Read more >How To Mock Super Class Method In Junit - Roy Tutorials
Here in this example I am going to show you how to mock super class method in Junit test class. A class that...
Read more >mockk-io/Lobby - Gitter
Hi all, I am trying to mock a class that has no default constructor and that it is provided as a dependecy to...
Read more >Mock constructors in code you don't own | MockK Guidebook
Rather than building a new instance every time the constructor is called, MockK generates a singleton and always returns the same instance. This...
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
Any update on this?. I’m also facing the same issue for both the
answers
andreturns
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. If you are sure that this issue is important and should not be marked as
stale
just ask to put animportant
label.