Skip to content

Spying on Spys does not work properly #1029

Description

@Vampire

To kill some PIT mutants, I added a Spy that verifies some condition and then uses callRealMethod() in a setup() method.

In some tests I also added a Spy on the same object that also calls callRealMethod().

so in the end it was like

testee.field = Spy(testee.field)
testee.field.someMethod() >> {
    // verify some condition here
    callRealMethod()
}
testee.field = Spy(testee.field)
testee.field.someMethod() >> {
    // verify some other condition here
    callRealMethod()
}

Now if testee.field.someMethod() is called I would have expected that the second spy tests "some other condition", then delegates to the first spy which tests "some condition" and then delegates to the real method.

Unfortunately, the first spy was skipped and after testing "some other condition" the second spy delegated to the real method directly.

This causes the mutant to not be killed properly (or rather a timeout due to a deadlock which costs more time than would have been necessary).

I worked around it by instead using a helper method that gets a closure, but it would be nice if it would actually work like expected. The workaround looks something like:

def prepareLocks(Closure someMethodInterceptor = { callRealMethod() }) {
    testee.field = Spy(testee.field)
    testee.field.someMethod() >> {
        // verify some condition here
        someMethodInterceptor.tap { it.delegate = owner.delegate }.call()
    }
}
def test() {
    given:
        prepareLocks {
            // verify some other condition here
            callRealMethod()
        }
    // other test content
}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions