|
1 | 1 | package com.instabug.reactlibrary; |
2 | | - |
3 | | -import android.os.Handler; |
4 | 2 | import android.os.Looper; |
5 | | -import android.os.SystemClock; |
6 | 3 |
|
7 | 4 | import com.facebook.react.bridge.Callback; |
8 | 5 | import com.instabug.apm.APM; |
9 | | -import com.instabug.apm.model.ExecutionTrace; |
10 | 6 |
|
11 | | -import com.facebook.react.bridge.Arguments; |
12 | | -import com.instabug.reactlibrary.utils.InstabugUtil; |
13 | 7 | import com.instabug.reactlibrary.utils.MainThreadHandler; |
14 | 8 |
|
15 | | -import org.json.JSONArray; |
| 9 | +import org.junit.After; |
16 | 10 | import org.junit.Before; |
17 | 11 | import org.junit.Test; |
18 | | -import org.junit.runner.RunWith; |
19 | | -import org.mockito.Matchers; |
20 | | -import org.mockito.internal.verification.VerificationModeFactory; |
| 12 | +import org.mockito.MockedStatic; |
21 | 13 | import org.mockito.invocation.InvocationOnMock; |
22 | 14 | import org.mockito.stubbing.Answer; |
23 | | -import org.powermock.api.mockito.PowerMockito; |
24 | | -import org.powermock.core.classloader.annotations.PrepareForTest; |
25 | | -import org.powermock.modules.junit4.PowerMockRunner; |
26 | 15 |
|
27 | 16 | import java.util.concurrent.Executors; |
28 | 17 | import java.util.concurrent.ScheduledExecutorService; |
29 | 18 |
|
30 | | -import static org.mockito.Matchers.any; |
31 | | -import static org.mockito.Matchers.anyLong; |
32 | | -import static org.mockito.Matchers.eq; |
| 19 | +import static org.mockito.ArgumentMatchers.any; |
| 20 | +import static org.mockito.Mockito.doAnswer; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.mockStatic; |
| 23 | +import static org.mockito.Mockito.times; |
33 | 24 | import static org.mockito.Mockito.verify; |
34 | | -import static org.powermock.api.mockito.PowerMockito.doAnswer; |
35 | | -import static org.powermock.api.mockito.PowerMockito.mock; |
36 | | -import static org.powermock.api.mockito.PowerMockito.when; |
37 | | - |
38 | | -@RunWith(PowerMockRunner.class) |
39 | | -@PrepareForTest({Looper.class, android.os.Handler.class, APM.class, ExecutionTrace.class, SystemClock.class, Runnable.class, RNInstabugAPMModule.class, Arguments.class, InstabugUtil.class, MainThreadHandler.class}) |
| 25 | +import static org.mockito.Mockito.when; |
40 | 26 |
|
41 | 27 | public class RNInstabugAPMModuleTest { |
42 | 28 |
|
43 | 29 | private RNInstabugAPMModule apmModule = new RNInstabugAPMModule(null); |
44 | | - |
45 | 30 | private final static ScheduledExecutorService mainThread = Executors.newSingleThreadScheduledExecutor(); |
46 | 31 |
|
| 32 | + // Mock Objects |
| 33 | + private MockedStatic<Looper> mockLooper; |
| 34 | + private MockedStatic <MainThreadHandler> mockMainThreadHandler; |
| 35 | + private MockedStatic <APM> mockAPM; |
| 36 | + |
47 | 37 | @Before |
48 | 38 | public void mockMainThreadHandler() throws Exception { |
49 | | - PowerMockito.mockStatic(Looper.class); |
| 39 | + // Mock static functions |
| 40 | + mockAPM = mockStatic(APM.class); |
| 41 | + mockLooper = mockStatic(Looper.class); |
| 42 | + mockMainThreadHandler = mockStatic(MainThreadHandler.class); |
| 43 | + |
| 44 | + // Mock Looper class |
50 | 45 | Looper mockMainThreadLooper = mock(Looper.class); |
51 | 46 | when(Looper.getMainLooper()).thenReturn(mockMainThreadLooper); |
52 | | - Handler mockMainThreadHandler = mock(Handler.class); |
| 47 | + |
| 48 | + // Override runOnMainThread |
53 | 49 | Answer<Boolean> handlerPostAnswer = new Answer<Boolean>() { |
54 | 50 | @Override |
55 | 51 | public Boolean answer(InvocationOnMock invocation) throws Throwable { |
56 | | - invocation.getArgumentAt(0, Runnable.class).run(); |
| 52 | + invocation.getArgument(0, Runnable.class).run(); |
57 | 53 | return true; |
58 | 54 | } |
59 | 55 | }; |
60 | | - doAnswer(handlerPostAnswer).when(mockMainThreadHandler).post(any(Runnable.class)); |
61 | | - doAnswer(handlerPostAnswer).when(mockMainThreadHandler).postDelayed(any(Runnable.class), anyLong()); |
62 | | - PowerMockito.whenNew(Handler.class).withArguments(mockMainThreadLooper).thenReturn(mockMainThreadHandler); |
| 56 | + doAnswer(handlerPostAnswer).when(MainThreadHandler.class); |
| 57 | + MainThreadHandler.runOnMainThread(any(Runnable.class)); |
| 58 | + } |
| 59 | + @After |
| 60 | + public void tearDown() { |
| 61 | + // Remove static mocks |
| 62 | + mockLooper.close(); |
| 63 | + mockMainThreadHandler.close(); |
| 64 | + mockAPM.close(); |
63 | 65 | } |
64 | 66 |
|
65 | 67 | /********APM*********/ |
66 | 68 |
|
67 | 69 | @Test |
68 | | - public void givenFalse$setEnabled_whenQuery_thenShouldCallNativeApiWithDisabled() { |
69 | | - // given |
70 | | - PowerMockito.mockStatic(APM.class); |
| 70 | + public void givenFalsesetEnabled_whenQuery_thenShouldCallNativeApiWithDisabled() { |
71 | 71 | // when |
72 | 72 | apmModule.setEnabled(false); |
73 | 73 | // then |
74 | | - PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 74 | + verify(APM.class, times(1)); |
75 | 75 | APM.setEnabled(false); |
76 | 76 | } |
77 | 77 |
|
78 | 78 | @Test |
79 | | - public void givenTrue$setEnabled_whenQuery_thenShouldCallNativeApiWithEnabled() { |
80 | | - // given |
81 | | - PowerMockito.mockStatic(APM.class); |
| 79 | + public void givenTruesetEnabled_whenQuery_thenShouldCallNativeApiWithEnabled() { |
82 | 80 | // when |
83 | 81 | apmModule.setEnabled(true); |
84 | 82 | // then |
85 | | - PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 83 | + verify(APM.class, times(1)); |
86 | 84 | APM.setEnabled(true); |
87 | 85 | } |
88 | 86 |
|
89 | 87 | @Test |
90 | 88 | public void givenFalse$setAppLaunchEnabled_whenQuery_thenShouldCallNativeApiWithDisabled() { |
91 | | - // given |
92 | | - PowerMockito.mockStatic(APM.class); |
| 89 | + |
93 | 90 | // when |
94 | 91 | apmModule.setAppLaunchEnabled(false); |
95 | 92 | // then |
96 | | - PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 93 | + verify(APM.class, times(1)); |
97 | 94 | APM.setAppLaunchEnabled(false); |
98 | 95 | } |
99 | 96 |
|
100 | 97 | @Test |
101 | 98 | public void givenTrue$setAppLaunchEnabled_whenQuery_thenShouldCallNativeApiWithEnabled() { |
102 | | - // given |
103 | | - PowerMockito.mockStatic(APM.class); |
| 99 | + |
104 | 100 | // when |
105 | 101 | apmModule.setAppLaunchEnabled(true); |
106 | 102 | // then |
107 | | - PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 103 | + verify(APM.class, times(1)); |
108 | 104 | APM.setAppLaunchEnabled(true); |
109 | 105 | } |
110 | 106 |
|
| 107 | + @Test |
| 108 | + public void given$endAppLaunch_whenQuery_thenShouldCallNativeApiWithEnabled() { |
| 109 | + |
| 110 | + // when |
| 111 | + apmModule.endAppLaunch(); |
| 112 | + // then |
| 113 | + verify(APM.class, times(1)); |
| 114 | + APM.endAppLaunch(); |
| 115 | + } |
| 116 | + |
111 | 117 | @Test |
112 | 118 | public void givenString$startExecutionTrace_whenQuery_thenShouldCallNativeApi() { |
113 | | - // given |
114 | | - PowerMockito.mockStatic(APM.class); |
| 119 | + |
115 | 120 | Callback callback = mock(Callback.class); |
116 | 121 | // when |
117 | 122 | apmModule.startExecutionTrace("trace", "1", callback); |
118 | 123 | // then |
119 | | - PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 124 | + verify(APM.class, times(1)); |
120 | 125 | APM.startExecutionTrace("trace"); |
121 | 126 | verify(callback).invoke(any()); |
122 | 127 | } |
@@ -151,24 +156,22 @@ public Boolean answer(InvocationOnMock invocation) throws Throwable { |
151 | 156 |
|
152 | 157 | @Test |
153 | 158 | public void givenString$startUITrace_whenQuery_thenShouldCallNativeApiWithEnabled() { |
154 | | - // given |
155 | | - PowerMockito.mockStatic(APM.class); |
| 159 | + |
156 | 160 | // when |
157 | 161 | apmModule.startUITrace("uiTrace"); |
158 | 162 | // then |
159 | | - PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 163 | + verify(APM.class, times(1)); |
160 | 164 | APM.startUITrace("uiTrace"); |
161 | 165 | } |
162 | 166 |
|
163 | 167 | @Test |
164 | 168 | public void given$endUITrace_whenQuery_thenShouldCallNativeApiWithEnabled() { |
165 | | - // given |
166 | | - PowerMockito.mockStatic(APM.class); |
| 169 | + |
167 | 170 | // when |
168 | 171 | apmModule.startUITrace("uiTrace"); |
169 | 172 | apmModule.endUITrace(); |
170 | 173 | // then |
171 | | - PowerMockito.verifyStatic(VerificationModeFactory.times(1)); |
| 174 | + verify(APM.class, times(1)); |
172 | 175 | APM.endUITrace(); |
173 | 176 | } |
174 | 177 | } |
0 commit comments