I have abstract class D which is a dependency of the tested class T.
The test class:
public class T_Test {
@Tested T tested;
D dependency;
public void test() {
dependency.doSomething();
tested.testedMethod(dependency);
}
}
I want the dependency.doSomething() will run the real code of this method, but that the abstract methods will be mocked.
If I run the test as is, I obviously get
NullPointerExceptionfor using the uninitializeddependency.If I add the
@Mockedannotation to theD dependencyline, all the methods inDare mocked, sod.doSomething()doesn't do what it's supposed to do.If I keep the
@Mockedannotation and also add an emptyNonStrictExpectationsblock at the beginning of the test method, in order to have partial mock, either like this:new NonStrictExpectations(D.class) {};or like this:
new NonStrictExpectations(d) {};I get
java.lang.IllegalArgumentException: Already mocked: class D.If I keep the
NonStrictExpectationsblock and remove the@Mockedannotation, again I getNullPointerExceptionfor using the uninitializeddependency.
So how can I partially mock this dependency abstract class?
Using the
@Capturingannotation on the dependency achieves this. No need to add an empty expectations block; only the abstract methods will be mocked.