View Javadoc
1   package mockit;
2   
3   import static org.junit.jupiter.api.Assertions.assertEquals;
4   
5   import java.io.Serializable;
6   
7   import mockit.MockingMultipleInterfacesTest.Dependency;
8   
9   import org.junit.jupiter.api.Test;
10  
11  /**
12   * The Class MockingMultipleInterfacesTest.
13   *
14   * @param <MultiMock>
15   *            the generic type
16   */
17  final class MockingMultipleInterfacesTest<MultiMock extends Dependency & Runnable> {
18  
19      /**
20       * The Interface Dependency.
21       */
22      interface Dependency {
23          /**
24           * Do something.
25           *
26           * @param b
27           *            the b
28           *
29           * @return the string
30           */
31          String doSomething(boolean b);
32      }
33  
34      /** The multi mock. */
35      @Mocked
36      MultiMock multiMock;
37  
38      /**
39       * Mock field with two interfaces.
40       */
41      @Test
42      void mockFieldWithTwoInterfaces() {
43          new Expectations() {
44              {
45                  multiMock.doSomething(false);
46                  result = "test";
47              }
48          };
49  
50          multiMock.run();
51          assertEquals("test", multiMock.doSomething(false));
52  
53          new Verifications() {
54              {
55                  multiMock.run();
56              }
57          };
58      }
59  
60      /**
61       * Mock parameter with two interfaces.
62       *
63       * @param <M>
64       *            the generic type
65       * @param mock
66       *            the mock
67       */
68      @Test
69      <M extends Dependency & Serializable> void mockParameterWithTwoInterfaces(@Mocked final M mock) {
70          new Expectations() {
71              {
72                  mock.doSomething(true);
73                  result = "test";
74              }
75          };
76  
77          assertEquals("test", mock.doSomething(true));
78      }
79  
80      /**
81       * The Interface Base.
82       */
83      public interface Base {
84          /**
85           * Do something.
86           */
87          void doSomething();
88      }
89  
90      /**
91       * The Class Derived.
92       */
93      abstract static class Derived implements Base {
94          /**
95           * Instantiates a new derived.
96           */
97          protected Derived() {
98          }
99      }
100 
101     /**
102      * The Class ToBeMocked.
103      */
104     public abstract static class ToBeMocked extends Derived {
105     }
106 
107     /**
108      * Mock abstract method inherited from interface implemented by super class.
109      *
110      * @param mock
111      *            the mock
112      */
113     @Test
114     void mockAbstractMethodInheritedFromInterfaceImplementedBySuperClass(@Mocked final ToBeMocked mock) {
115         mock.doSomething();
116 
117         new Verifications() {
118             {
119                 mock.doSomething();
120                 times = 1;
121             }
122         };
123     }
124 }