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