How to use InvocationContainerImpl class of org.mockito.internal.stubbing package

Best Mockito code snippet using org.mockito.internal.stubbing.InvocationContainerImpl

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

copy
1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal;
6
7import java.util.List;
8
9import org.mockito.internal.creation.MockSettingsImpl;
10import org.mockito.internal.invocation.Invocation;
11import org.mockito.internal.invocation.InvocationMatcher;
12import org.mockito.internal.invocation.MatchersBinder;
13import org.mockito.internal.progress.MockingProgress;
14import org.mockito.internal.progress.ThreadSafeMockingProgress;
15import org.mockito.internal.stubbing.InvocationContainer;
16import org.mockito.internal.stubbing.InvocationContainerImpl;
17import org.mockito.internal.stubbing.OngoingStubbingImpl;
18import org.mockito.internal.stubbing.StubbedInvocationMatcher;
19import org.mockito.internal.stubbing.VoidMethodStubbableImpl;
20import org.mockito.internal.verification.MockAwareVerificationMode;
21import org.mockito.internal.verification.VerificationDataImpl;
22import org.mockito.stubbing.Answer;
23import org.mockito.stubbing.VoidMethodStubbable;
24import org.mockito.verification.VerificationMode;
25
26/**
27 * Invocation handler set on mock objects.
28 * 
29 * @param <T>
30 *            type of mock object to handle
31 */
32public class MockHandler<T> implements MockitoInvocationHandler, MockHandlerInterface<T> {
33
34    private static final long serialVersionUID = -2917871070982574165L;
35
36    InvocationContainerImpl invocationContainerImpl;
37    MatchersBinder matchersBinder = new MatchersBinder();
38    MockingProgress mockingProgress = new ThreadSafeMockingProgress();
39
40    private final MockSettingsImpl mockSettings;
41
42    public MockHandler(MockSettingsImpl mockSettings) {
43        this.mockSettings = mockSettings;
44        this.mockingProgress = new ThreadSafeMockingProgress();
45        this.matchersBinder = new MatchersBinder();
46        this.invocationContainerImpl = new InvocationContainerImpl(mockingProgress);
47    }
48
49    // for tests
50    MockHandler() {
51        this(new MockSettingsImpl());
52    }
53
54    public MockHandler(MockHandlerInterface<T> oldMockHandler) {
55        this(oldMockHandler.getMockSettings());
56    }
57
58    public Object handle(Invocation invocation) throws Throwable {
59        if (invocationContainerImpl.hasAnswersForStubbing()) {
60            // stubbing voids with stubVoid() or doAnswer() style
61            InvocationMatcher invocationMatcher = matchersBinder.bindMatchers(mockingProgress
62                            .getArgumentMatcherStorage(), invocation);
63            invocationContainerImpl.setMethodForStubbing(invocationMatcher);
64            return null;
65        }
66        VerificationMode verificationMode = mockingProgress.pullVerificationMode();
67
68        InvocationMatcher invocationMatcher = matchersBinder.bindMatchers(mockingProgress.getArgumentMatcherStorage(),
69                        invocation);
70
71        mockingProgress.validateState();
72
73        //if verificationMode is not null then someone is doing verify()        
74        if (verificationMode != null) {
75            //We need to check if verification was started on the correct mock 
76            // - see VerifyingWithAnExtraCallToADifferentMockTest (bug 138)
77            //TODO: can I avoid this cast here?
78            if (((MockAwareVerificationMode) verificationMode).getMock() == invocation.getMock()) {                
79                VerificationDataImpl data = new VerificationDataImpl(invocationContainerImpl, invocationMatcher);            
80                verificationMode.verify(data);
81                return null;
82            } else {
83                // this means there is an invocation on a different mock. Re-adding verification mode 
84                // - see VerifyingWithAnExtraCallToADifferentMockTest (bug 138)
85                mockingProgress.verificationStarted(verificationMode);
86            }
87        }
88        
89        invocationContainerImpl.setInvocationForPotentialStubbing(invocationMatcher);
90        OngoingStubbingImpl<T> ongoingStubbing = new OngoingStubbingImpl<T>(invocationContainerImpl);
91        mockingProgress.reportOngoingStubbing(ongoingStubbing);
92
93        StubbedInvocationMatcher stubbedInvocation = invocationContainerImpl.findAnswerFor(invocation);
94
95        if (stubbedInvocation != null) {
96            stubbedInvocation.captureArgumentsFrom(invocation);
97            return stubbedInvocation.answer(invocation);
98        } else {
99            Object ret = mockSettings.getDefaultAnswer().answer(invocation);
100
101            // redo setting invocation for potential stubbing in case of partial
102            // mocks / spies.
103            // Without it, the real method inside 'when' might have delegated
104            // to other self method and overwrite the intended stubbed method
105            // with a different one. The reset is required to avoid runtime exception that validates return type with stubbed method signature.
106            invocationContainerImpl.resetInvocationForPotentialStubbing(invocationMatcher);
107            return ret;
108        }
109    }
110
111    public VoidMethodStubbable<T> voidMethodStubbable(T mock) {
112        return new VoidMethodStubbableImpl<T>(mock, invocationContainerImpl);
113    }
114
115    public MockSettingsImpl getMockSettings() {
116        return mockSettings;
117    }
118
119    @SuppressWarnings("unchecked")
120    public void setAnswersForStubbing(List<Answer> answers) {
121        invocationContainerImpl.setAnswersForStubbing(answers);
122    }
123
124    public InvocationContainer getInvocationContainer() {
125        return invocationContainerImpl;
126    }
127}
Full Screen
copy
1/*
2 * Copyright (c) 2007 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.internal.stubbing;
6
7import org.mockito.internal.invocation.Invocation;
8import org.mockito.stubbing.Answer;
9import org.mockito.stubbing.DeprecatedOngoingStubbing;
10import org.mockito.stubbing.OngoingStubbing;
11
12import java.util.List;
13
14public class OngoingStubbingImpl<T> extends BaseStubbing<T> {
15    
16    private final InvocationContainerImpl invocationContainerImpl;
17
18    public OngoingStubbingImpl(InvocationContainerImpl invocationContainerImpl) {
19        this.invocationContainerImpl = invocationContainerImpl;
20    }
21
22    public OngoingStubbing<T> thenAnswer(Answer<?> answer) {
23        invocationContainerImpl.addAnswer(answer);
24        return new ConsecutiveStubbing<T>(invocationContainerImpl);
25    }
26
27    public DeprecatedOngoingStubbing<T> toAnswer(Answer<?> answer) {
28        invocationContainerImpl.addAnswer(answer);
29        return new ConsecutiveStubbing<T>(invocationContainerImpl);
30    }
31
32    public List<Invocation> getRegisteredInvocations() {
33        //TODO interface for tests
34        return invocationContainerImpl.getInvocations();
35    }
36}
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

...Most popular Stackoverflow questions on InvocationContainerImpl

    No relevant questions found for this class 😞

Run Selenium Automation Tests on LambdaTest Cloud Grid

Trigger Selenium automation tests on a cloud-based Grid of 3000+ real browsers and operating systems.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)