下面列出了怎么用org.mockito.internal.stubbing.InvocationContainerImpl的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* Verify the same invocations have been applied to two mocks. This is generally not
* the preferred way test with mockito and should be avoided if possible.
* @param expected the mock containing expected invocations
* @param actual the mock containing actual invocations
* @param argumentAdapters adapters that can be used to change argument values before they are compared
*/
public static <T> void verifySameInvocations(T expected, T actual, InvocationArgumentsAdapter... argumentAdapters) {
List<Invocation> expectedInvocations =
((InvocationContainerImpl) MockUtil.getMockHandler(expected).getInvocationContainer()).getInvocations();
List<Invocation> actualInvocations =
((InvocationContainerImpl) MockUtil.getMockHandler(actual).getInvocationContainer()).getInvocations();
verifySameInvocations(expectedInvocations, actualInvocations, argumentAdapters);
}
/**
* Verify the same invocations have been applied to two mocks. This is generally not
* the preferred way test with mockito and should be avoided if possible.
* @param expected the mock containing expected invocations
* @param actual the mock containing actual invocations
* @param argumentAdapters adapters that can be used to change argument values before they are compared
*/
public static <T> void verifySameInvocations(T expected, T actual, InvocationArgumentsAdapter... argumentAdapters) {
List<Invocation> expectedInvocations =
((InvocationContainerImpl) MockUtil.getMockHandler(expected).getInvocationContainer()).getInvocations();
List<Invocation> actualInvocations =
((InvocationContainerImpl) MockUtil.getMockHandler(actual).getInvocationContainer()).getInvocations();
verifySameInvocations(expectedInvocations, actualInvocations, argumentAdapters);
}
private Object deepStub(InvocationOnMock invocation, GenericMetadataSupport returnTypeGenericMetadata) throws Throwable {
InternalMockHandler<Object> handler = new MockUtil().getMockHandler(invocation.getMock());
InvocationContainerImpl container = (InvocationContainerImpl) handler.getInvocationContainer();
// matches invocation for verification
for (StubbedInvocationMatcher stubbedInvocationMatcher : container.getStubbedInvocations()) {
if(container.getInvocationForStubbing().matches(stubbedInvocationMatcher.getInvocation())) {
return stubbedInvocationMatcher.answer(invocation);
}
}
// record deep stub answer
return recordDeepStubAnswer(newDeepStubMock(returnTypeGenericMetadata), container);
}
public MockHandler(MockSettingsImpl mockSettings) {
this.mockSettings = mockSettings;
this.mockingProgress = new ThreadSafeMockingProgress();
this.matchersBinder = new MatchersBinder();
this.invocationContainerImpl = new InvocationContainerImpl(mockingProgress);
}
private Object recordDeepStubAnswer(final Object mock, InvocationContainerImpl container) throws Throwable {
container.addAnswer(new DeeplyStubbedAnswer(mock), false);
return mock;
}
private void stubOrdinaryInvocationWithInvocationMatcher(MockHandlerImpl<?> handler, StubbedInvocationMatcher value) {
handler.invocationContainerImpl = mock(InvocationContainerImpl.class);
given(handler.invocationContainerImpl.findAnswerFor(any(InvocationImpl.class))).willReturn(value);
}
@After
public void printLogs() {
((InvocationContainerImpl) getMockHandler(log).getInvocationContainer()).getInvocations().forEach(System.out::println);
}