Search code examples
javaandroidtddjmockit

Can I pass a jmockit mock of an interface as a constructor parameter of another object?


i'm playing around with jmockit. Most of what I tried(with the help of the web) works splendid.

But it seems, like there is a problem when passing a reference to a

@Mocked
SomeInterface someInterface

as a parameter to a constructor of object under test. The test run shows, that the instance is null.

Any ideas?

Much appreciated.


Solution

  • Use @Capturing instead of @Mocked.

    @Mocked only mocks all instances of the given class and its super-classes but not the sub-classes or implementations of interfaces. That's what @Capturing is for. (see Capturing implementation classes and instances)