How to use MockHandlerImplTest class of org.mockito.internal.handler package

Best Mockito code snippet using org.mockito.internal.handler.MockHandlerImplTest

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.handler;
6
7
8import org.junit.Assert;
9import org.junit.Test;
10import org.mockito.ArgumentMatchers;
11import org.mockito.BDDMockito;
12import org.mockito.Mockito;
13import org.mockito.exceptions.base.MockitoException;
14import org.mockito.exceptions.misusing.InvalidUseOfMatchersException;
15import org.mockito.exceptions.misusing.WrongTypeOfReturnValue;
16import org.mockito.internal.creation.MockSettingsImpl;
17import org.mockito.internal.invocation.InvocationBuilder;
18import org.mockito.internal.invocation.InvocationMatcher;
19import org.mockito.internal.invocation.MatchersBinder;
20import org.mockito.internal.progress.ArgumentMatcherStorage;
21import org.mockito.internal.progress.ThreadSafeMockingProgress;
22import org.mockito.internal.stubbing.StubbedInvocationMatcher;
23import org.mockito.internal.stubbing.answers.Returns;
24import org.mockito.internal.verification.VerificationModeFactory;
25import org.mockito.invocation.Invocation;
26import org.mockito.listeners.InvocationListener;
27import org.mockito.listeners.MethodInvocationReport;
28import org.mockitoutil.TestBase;
29
30
31@SuppressWarnings({ "unchecked", "serial" })
32public class MockHandlerImplTest extends TestBase {
33    private StubbedInvocationMatcher stubbedInvocationMatcher = Mockito.mock(StubbedInvocationMatcher.class);
34
35    private Invocation invocation = Mockito.mock(Invocation.class);
36
37    @Test
38    public void should_remove_verification_mode_even_when_invalid_matchers() throws Throwable {
39        // given
40        Invocation invocation = new InvocationBuilder().toInvocation();
41        @SuppressWarnings("rawtypes")
42        MockHandlerImpl<?> handler = new MockHandlerImpl(new MockSettingsImpl());
43        ThreadSafeMockingProgress.mockingProgress().verificationStarted(VerificationModeFactory.atLeastOnce());
44        handler.matchersBinder = new MatchersBinder() {
45            public InvocationMatcher bindMatchers(ArgumentMatcherStorage argumentMatcherStorage, Invocation invocation) {
46                throw new InvalidUseOfMatchersException();
47            }
48        };
49        try {
50            // when
51            handler.handle(invocation);
52            // then
53            Assert.fail();
54        } catch (InvalidUseOfMatchersException ignored) {
55        }
56        Assert.assertNull(ThreadSafeMockingProgress.mockingProgress().pullVerificationMode());
57    }
58
59    @Test(expected = MockitoException.class)
60    public void should_throw_mockito_exception_when_invocation_handler_throws_anything() throws Throwable {
61        // given
62        InvocationListener throwingListener = Mockito.mock(InvocationListener.class);
63        Mockito.doThrow(new Throwable()).when(throwingListener).reportInvocation(ArgumentMatchers.any(MethodInvocationReport.class));
64        MockHandlerImpl<?> handler = create_correctly_stubbed_handler(throwingListener);
65        // when
66        handler.handle(invocation);
67    }
68
69    @Test(expected = WrongTypeOfReturnValue.class)
70    public void should_report_bogus_default_answer() throws Throwable {
71        MockSettingsImpl mockSettings = Mockito.mock(MockSettingsImpl.class);
72        MockHandlerImpl<?> handler = new MockHandlerImpl(mockSettings);
73        BDDMockito.given(mockSettings.getDefaultAnswer()).willReturn(new Returns(MockHandlerImplTest.AWrongType.WRONG_TYPE));
74        // otherwise cast is not done
75        @SuppressWarnings("unused")
76        String there_should_not_be_a_CCE_here = ((String) (handler.handle(new InvocationBuilder().method(Object.class.getDeclaredMethod("toString")).toInvocation())));
77    }
78
79    private static class AWrongType {
80        public static final MockHandlerImplTest.AWrongType WRONG_TYPE = new MockHandlerImplTest.AWrongType();
81    }
82}
83
84
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.concurrentmockito;
6
7import org.junit.Test;
8import org.junit.runner.JUnitCore;
9import org.junit.runner.Result;
10import org.junit.runner.notification.Failure;
11import org.mockito.MockitoTest;
12import org.mockito.exceptions.ReporterTest;
13import org.mockito.exceptions.base.MockitoAssertionErrorTest;
14import org.mockito.exceptions.base.MockitoExceptionTest;
15import org.mockito.internal.AllInvocationsFinderTest;
16import org.mockito.internal.InvalidStateDetectionTest;
17import org.mockito.internal.creation.jmock.ClassImposterizerTest;
18import org.mockito.internal.handler.MockHandlerImplTest;
19import org.mockito.internal.invocation.InvocationImplTest;
20import org.mockito.internal.invocation.InvocationMatcherTest;
21import org.mockito.internal.invocation.InvocationsFinderTest;
22import org.mockito.internal.matchers.ComparableMatchersTest;
23import org.mockito.internal.matchers.EqualsTest;
24import org.mockito.internal.matchers.MatchersToStringTest;
25import org.mockito.internal.progress.MockingProgressImplTest;
26import org.mockito.internal.progress.TimesTest;
27import org.mockito.internal.stubbing.defaultanswers.ReturnsEmptyValuesTest;
28import org.mockito.internal.util.MockUtilTest;
29import org.mockito.internal.util.collections.ListUtilTest;
30import org.mockito.internal.verification.RegisteredInvocationsTest;
31import org.mockito.internal.verification.checkers.MissingInvocationCheckerTest;
32import org.mockito.internal.verification.checkers.MissingInvocationInOrderCheckerTest;
33import org.mockito.internal.verification.checkers.NumberOfInvocationsCheckerTest;
34import org.mockito.internal.verification.checkers.NumberOfInvocationsInOrderCheckerTest;
35import org.mockitousage.basicapi.ReplacingObjectMethodsTest;
36import org.mockitousage.basicapi.ResetTest;
37import org.mockitousage.basicapi.UsingVarargsTest;
38import org.mockitousage.examples.use.ExampleTest;
39import org.mockitousage.matchers.CustomMatchersTest;
40import org.mockitousage.matchers.InvalidUseOfMatchersTest;
41import org.mockitousage.matchers.MatchersTest;
42import org.mockitousage.matchers.VerificationAndStubbingUsingMatchersTest;
43import org.mockitousage.misuse.InvalidUsageTest;
44import org.mockitousage.puzzlers.BridgeMethodPuzzleTest;
45import org.mockitousage.puzzlers.OverloadingPuzzleTest;
46import org.mockitousage.stacktrace.ClickableStackTracesTest;
47import org.mockitousage.stacktrace.PointingStackTraceToActualInvocationTest;
48import org.mockitousage.stacktrace.StackTraceFilteringTest;
49import org.mockitousage.stubbing.BasicStubbingTest;
50import org.mockitousage.stubbing.ReturningDefaultValuesTest;
51import org.mockitousage.stubbing.StubbingWithThrowablesTest;
52import org.mockitousage.verification.*;
53import org.mockitoutil.TestBase;
54
55import java.util.LinkedList;
56import java.util.List;
57
58public class ThreadsRunAllTestsHalfManualTest extends TestBase {
59    
60    private static class AllTestsRunner extends Thread {
61        
62        private boolean failed;
63
64        public void run() {
65            Result result = JUnitCore.runClasses(
66                    EqualsTest.class,
67                    ListUtilTest.class,
68                    MockingProgressImplTest.class,
69                    TimesTest.class,
70                    MockHandlerImplTest.class,
71                    AllInvocationsFinderTest.class,
72                    ReturnsEmptyValuesTest.class,
73                    NumberOfInvocationsCheckerTest.class,
74                    RegisteredInvocationsTest.class,
75                    MissingInvocationCheckerTest.class,
76                    NumberOfInvocationsInOrderCheckerTest.class,
77                    MissingInvocationInOrderCheckerTest.class,
78                    ClassImposterizerTest.class,
79                    InvocationMatcherTest.class,
80                    InvocationsFinderTest.class,
81                    InvocationImplTest.class,
82                    MockitoTest.class,
83                    MockUtilTest.class,
84                    ReporterTest.class,
85                    MockitoAssertionErrorTest.class,
86                    MockitoExceptionTest.class,
87                    StackTraceFilteringTest.class,
88                    BridgeMethodPuzzleTest.class,
89                    OverloadingPuzzleTest.class,
90                    InvalidUsageTest.class,
91                    UsingVarargsTest.class,
92                    CustomMatchersTest.class,
93                    ComparableMatchersTest.class,
94                    InvalidUseOfMatchersTest.class,
95                    MatchersTest.class,
96                    MatchersToStringTest.class,
97                    VerificationAndStubbingUsingMatchersTest.class,
98                    BasicStubbingTest.class,
99                    ReturningDefaultValuesTest.class,
100                    StubbingWithThrowablesTest.class,
101                    AtMostXVerificationTest.class,
102                    BasicVerificationTest.class,
103                    ExactNumberOfTimesVerificationTest.class,
104                    VerificationInOrderTest.class,
105                    NoMoreInteractionsVerificationTest.class,
106                    SelectedMocksInOrderVerificationTest.class,
107                    VerificationOnMultipleMocksUsingMatchersTest.class,
108                    VerificationUsingMatchersTest.class,
109                    RelaxedVerificationInOrderTest.class,
110                    DescriptiveMessagesWhenVerificationFailsTest.class,
111                    DescriptiveMessagesWhenTimesXVerificationFailsTest.class,
112                    BasicVerificationInOrderTest.class,
113                    VerificationInOrderMixedWithOrdiraryVerificationTest.class,
114                    DescriptiveMessagesOnVerificationInOrderErrorsTest.class,
115                    InvalidStateDetectionTest.class,
116                    ReplacingObjectMethodsTest.class,
117                    ClickableStackTracesTest.class,
118                    ExampleTest.class,
119                    PointingStackTraceToActualInvocationTest.class,
120                    VerificationInOrderFromMultipleThreadsTest.class,
121                    ResetTest.class
122                );
123                
124                if (!result.wasSuccessful()) {
125                    System.err.println("Thread[" + Thread.currentThread().getId() + "]: error!");
126                    List<Failure> failures = result.getFailures();
127                    System.err.println(failures.size());
128                    for (Failure failure : failures) {
129                        System.err.println(failure.getTrace());
130                        failed = true;
131                    }
132                }
133        }
134
135        public boolean isFailed() {
136            return failed;
137        }
138    }
139    
140    @Test
141    public void shouldRunInMultipleThreads() throws Exception {
142        //this test ALWAYS fails if there is a single failing unit
143        assertFalse("Run in multiple thread failed", runInMultipleThreads(3));
144    }
145    
146    public static boolean runInMultipleThreads(int numberOfThreads) throws Exception {
147        List<AllTestsRunner> threads = new LinkedList<AllTestsRunner>();
148        for (int i = 1; i <= numberOfThreads; i++) {
149            threads.add(new AllTestsRunner());
150        }
151
152        for (Thread t : threads) {
153            t.start();
154        }
155
156        boolean failed = false;
157        for (AllTestsRunner t : threads) {
158            t.join();
159            failed = failed ? true : t.isFailed();
160        }
161        
162        return failed;
163    }
164    
165    public static void main(String[] args) throws Exception {
166        int numberOfThreads = 20; 
167        long before = System.currentTimeMillis();
168        runInMultipleThreads(numberOfThreads);
169        long after = System.currentTimeMillis();
170        long executionTime = (after-before)/1000;
171        System.out.println("Finished tests in " + numberOfThreads + " threads in " + executionTime + " seconds.");
172    }
173}
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 MockHandlerImplTest

    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)