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