首先,您应该spy
作为构造函数参数传入。
除此之外,这就是你如何做到的。
public class ResultCaptor<T> implements Answer {
private T result = null;
public T getResult() {
return result;
}
@Override
public T answer(InvocationOnMock invocationOnMock) throws Throwable {
result = (T) invocationOnMock.callRealMethod();
return result;
}
}
预期用途:
RealFactory factory = new RealFactory();
RealFactory spy = spy(factory);
TestedClass testedClass = new TestedClass(spy);
// At this point I would like to get a reference to the object created
// and returned by the factory.
// let's capture the return values from spy.create()
ResultCaptor<RealThing> resultCaptor = new ResultCaptor<>();
doAnswer(resultCaptor).when(spy).create();
// do something that will trigger a call to the factory
testedClass.doSomething();
// validate the return object
assertThat(resultCaptor.getResult())
.isNotNull()
.isInstanceOf(RealThing.class);