0

I have the following code:

def method() {
    try {
        dependency0.call({ arg -> })
    } catch {
        dependency1.call()
    }
}

and the following test:

@Test
void shouldDoSomething(
        @Mocked final Dependency0 dependency0Mock) {
    final dependency1Mock = getDependency1Mock()

    new Expectations() {{
        dependency0Mock.call((Closure) any)
        result = new Exception('expected')

    final sut = new Sut(dependency1Mock, dependency0Mock)

    sut.method()
}

When the test runs, it emits the following exception:

mockit.internal.UnexpectedInvocation: Parameter "arg" of Dependency0#call(groovy.lang.Closure arg) expected null, got Sut$_method_closure1@cafebead

How do I go about mocking a method that takes in a Groovy Closure?

UPDATE: The same exception is thrown even if the argument type is Integer.

Noel Yap
  • 18,822
  • 21
  • 92
  • 144

1 Answers1

0

Don't use JMockit. Rather, use map coercion as described at http://johnpwood.net/2008/06/13/overriding-java-methods-in-groovy-for-unit-testing/.

Noel Yap
  • 18,822
  • 21
  • 92
  • 144
  • This does not work for constructors which takes argument as parameters. In that case mock/stubs from Groovy are not enough. – Abbadon Apr 18 '17 at 06:30