Constructor mock behaviour change in 1.11.0
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
Updating from 1.10.6 to 1.11.0 and the behaviour of constructor mocks seems to have changed with the new functionality which leads to code that used to work in 1.10.6 now breaking
Current Behavior
The following code will pass in 1.10.6 but fail in 1.11.0
class Voice {
fun speak() = "HELLO"
}
class Animal {
private val voice = Voice()
fun speak() = voice.speak()
}
@Test
fun demo() {
mockkConstructor(Voice::class)
val animal = Animal()
every { anyConstructed<Voice>().speak() } answers { "woof" }
val actual = animal.speak()
assertEquals("woof", actual)
}
Context
Please provide any relevant information about your setup. This is important in case the issue is not reproducible except for under certain conditions.
- OS: MacOS
- Kotlin version: 1.4.31
- JDK version: 11
- JUnit version: 5.7.1
- Type of test: unit test
Issue Analytics
- State:
- Created 3 years ago
- Reactions:13
- Comments:12
Top Results From Across the Web
how to change mocked function behaviour? - Stack Overflow
WillByDefault() , they should be set in the constructor or anywhere before method calls, similarly above: MockReal() { ON_CALL(*this, run).
Read more >Mock Java Constructors With Mockito | Configuration ... - rieckpil
Learn how to mock Java constructors to return mock objects using Mockito (with version >= 3.5.0) and the InlineMockMaker.
Read more >mockito | Dart Package - Pub.dev
A mock framework inspired by Mockito with APIs for Fakes, Mocks, behavior verification, and stubbing.
Read more >Micronaut Test
Use mocks to replace existing beans for the scope of a test suite ... This behaviour can be changed by using the transactional...
Read more >Changelog - Sinon.JS
Standalone test spies, stubs and mocks for JavaScript. Works with any unit testing framework.
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 FreeTop 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
Top GitHub Comments
I can confirm this still occurs in 1.12
For what it’s worth, simply changing
anyConstructor
toconstructedWith
worked for me in 1.12.1. Small snippet of before/after:OS: MacOS Kotlin version: 1.6.0 JDK version: 17 JUnit version: 5.8.1 Type of test: unit test