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.

Constructor mock behaviour change in 1.11.0

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

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:open
  • Created 3 years ago
  • Reactions:13
  • Comments:12

github_iconTop GitHub Comments

5reactions
ValCanBuildcommented, Aug 3, 2021

I can confirm this still occurs in 1.12

1reaction
phillipuniversecommented, Feb 25, 2022

For what it’s worth, simply changing anyConstructor to constructedWith worked for me in 1.12.1. Small snippet of before/after:

val workflowPassedToConstraintValidationSlot = slot<Workflow>()
verify(exactly = 1) {
    anyConstructor<WorkflowStepInputsArePresentConstraintValidator>().isValid(capture(workflowPassedToConstraintValidationSlot), any())
}
assertThat(workflowPassedToConstraintValidationSlot.captured.name).isEqualTo("hey i changed your name")
val workflowPassedToConstraintValidationSlot = slot<Workflow>()
verify(exactly = 1) {
    constructedWith<WorkflowStepInputsArePresentConstraintValidator>().isValid(capture(workflowPassedToConstraintValidationSlot), any())
}
assertThat(workflowPassedToConstraintValidationSlot.captured.name).isEqualTo("hey i changed your name")

OS: MacOS Kotlin version: 1.6.0 JDK version: 17 JUnit version: 5.8.1 Type of test: unit test

Read more comments on GitHub >

github_iconTop 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 >

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