How to use method of org.mockito.InvocationFactoryTest class

Best Mockito code snippet using org.mockito.InvocationFactoryTest.

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1/**
2 * Copyright (c) 2018 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito;
6
7
8import java.util.concurrent.Callable;
9import org.junit.Assert;
10import org.junit.Test;
11import org.mockito.invocation.Invocation;
12import org.mockito.invocation.InvocationFactory;
13import org.mockitoutil.TestBase;
14
15
16public class InvocationFactoryTest extends TestBase {
17    static class TestClass {
18        public String testMethod() throws Throwable {
19            return "un-mocked";
20        }
21    }
22
23    final InvocationFactoryTest.TestClass mock = Mockito.spy(InvocationFactoryTest.TestClass.class);
24
25    @Test
26    public void call_method_that_throws_a_throwable() throws Throwable {
27        Invocation invocation = Mockito.framework().getInvocationFactory().createInvocation(mock, Mockito.withSettings().build(InvocationFactoryTest.TestClass.class), InvocationFactoryTest.TestClass.class.getDeclaredMethod("testMethod"), new InvocationFactory.RealMethodBehavior() {
28            @Override
29            public Object call() throws Throwable {
30                throw new Throwable("mocked");
31            }
32        });
33        try {
34            Mockito.mockingDetails(mock).getMockHandler().handle(invocation);
35        } catch (Throwable t) {
36            Assert.assertEquals("mocked", t.getMessage());
37            return;
38        }
39        Assert.fail();
40    }
41
42    @Test
43    public void call_method_that_returns_a_string() throws Throwable {
44        Invocation invocation = Mockito.framework().getInvocationFactory().createInvocation(mock, Mockito.withSettings().build(InvocationFactoryTest.TestClass.class), InvocationFactoryTest.TestClass.class.getDeclaredMethod("testMethod"), new InvocationFactory.RealMethodBehavior() {
45            @Override
46            public Object call() throws Throwable {
47                return "mocked";
48            }
49        });
50        Object ret = Mockito.mockingDetails(mock).getMockHandler().handle(invocation);
51        Assert.assertEquals("mocked", ret);
52    }
53
54    @Test
55    public void deprecated_api_still_works() throws Throwable {
56        Invocation invocation = Mockito.framework().getInvocationFactory().createInvocation(mock, Mockito.withSettings().build(InvocationFactoryTest.TestClass.class), InvocationFactoryTest.TestClass.class.getDeclaredMethod("testMethod"), new Callable() {
57            public Object call() throws Exception {
58                return "mocked";
59            }
60        });
61        Object ret = Mockito.mockingDetails(mock).getMockHandler().handle(invocation);
62        Assert.assertEquals("mocked", ret);
63    }
64}
65
66
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

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

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)