19
19
import static org .junit .Assert .assertEquals ;
20
20
import static org .junit .Assert .assertFalse ;
21
21
import static org .junit .Assert .assertNull ;
22
- import static org .mockito .Matchers .any ;
23
- import static org .mockito .Matchers .anyString ;
22
+ import static org .mockito .ArgumentMatchers .any ;
23
+ import static org .mockito .ArgumentMatchers .anyString ;
24
24
import static org .mockito .Mockito .mock ;
25
25
import static org .mockito .Mockito .when ;
26
+ import static org .mockito .Mockito .*;
27
+
28
+ import org .mockito .MockedStatic ;
29
+
26
30
27
31
import android .content .Context ;
28
32
35
39
import org .junit .Test ;
36
40
import org .junit .runner .RunWith ;
37
41
import org .mockito .Mockito ;
38
- import org .powermock .api .mockito .PowerMockito ;
39
- import org .powermock .core .classloader .annotations .PowerMockIgnore ;
40
- import org .powermock .core .classloader .annotations .PrepareForTest ;
41
- import org .powermock .modules .junit4 .PowerMockRunner ;
42
+ import org .mockito .junit .MockitoJUnitRunner ;
42
43
43
44
import java .io .IOException ;
44
45
45
46
/**
46
47
* Tests {@link EventWorker}
47
48
*/
48
- @ RunWith (PowerMockRunner .class )
49
- @ PrepareForTest ({ EventWorker .class , EventHandlerUtils .class , WorkerParameters .class })
50
- @ PowerMockIgnore ("jdk.internal.reflect.*" )
49
+ @ RunWith (MockitoJUnitRunner .class )
51
50
public class EventWorkerUnitTest {
52
51
53
- private WorkerParameters mockWorkParams = PowerMockito . mock (WorkerParameters .class );
52
+ private WorkerParameters mockWorkParams = mock (WorkerParameters .class );
54
53
private EventWorker eventWorker = new EventWorker (mock (Context .class ), mockWorkParams );
55
54
56
55
private String host = "http://www.foo.com" ;
@@ -72,31 +71,40 @@ public void dataForCompressedEvent() {
72
71
assertEquals (data .getString ("bodyCompressed" ), base64 );
73
72
assertNull (data .getString ("body" ));
74
73
}
75
-
76
74
@ Test
77
75
public void compressEvent () throws IOException {
78
76
String base64 = "abc123" ;
79
- PowerMockito .mockStatic (EventHandlerUtils .class );
80
- when (EventHandlerUtils .compress (anyString ())).thenReturn (base64 );
81
77
82
- Data data = EventWorker .compressEvent (host , smallBody );
83
- assertEquals (data .getString ("url" ), host );
84
- assertEquals (data .getString ("bodyCompressed" ), base64 );
85
- assertNull (data .getString ("body" ));
78
+ // Mocking the static method compress in EventHandlerUtils
79
+ try (MockedStatic <EventHandlerUtils > mockedStatic = mockStatic (EventHandlerUtils .class )) {
80
+ mockedStatic .when (() -> EventHandlerUtils .compress (anyString ())).thenReturn (base64 );
81
+
82
+ Data data = EventWorker .compressEvent (host , smallBody );
83
+
84
+ // Verify the results
85
+ assertEquals (data .getString ("url" ), host );
86
+ assertEquals (data .getString ("bodyCompressed" ), base64 );
87
+ assertNull (data .getString ("body" ));
88
+
89
+ // Optionally, verify that the method was called
90
+ mockedStatic .verify (() -> EventHandlerUtils .compress (anyString ()));
91
+ }
86
92
}
87
93
94
+
88
95
@ Test
89
96
public void compressEventWithCompressionFailure () throws IOException {
90
- PowerMockito .mockStatic (EventHandlerUtils .class );
91
- PowerMockito .doThrow (new IOException ()).when (EventHandlerUtils .class );
92
- EventHandlerUtils .compress (anyString ()); // PowerMockito throws exception on this static method
97
+ try (MockedStatic <EventHandlerUtils > mockedStatic = mockStatic (EventHandlerUtils .class )) {
98
+ mockedStatic .when (() -> EventHandlerUtils .compress (anyString ())).thenThrow (new IOException ());
93
99
94
- // return original body if compress fails
100
+ // return original body if compress fails
95
101
96
- Data data = EventWorker .compressEvent (host , smallBody );
97
- assertEquals (data .getString ("url" ), host );
98
- assertEquals (data .getString ("body" ), smallBody );
99
- assertNull (data .getByteArray ("bodyCompressed" ));
102
+ Data data = EventWorker .compressEvent (host , smallBody );
103
+ assertEquals (data .getString ("url" ), host );
104
+ assertEquals (data .getString ("body" ), smallBody );
105
+ assertNull (data .getByteArray ("bodyCompressed" ));
106
+
107
+ }
100
108
}
101
109
102
110
@ Test
@@ -155,12 +163,17 @@ public void getEventBodyFromInputDataCompressed() {
155
163
public void getEventBodyFromInputDataDecompressFailure () throws Exception {
156
164
Data data = EventWorker .compressEvent (host , smallBody );
157
165
158
- PowerMockito .mockStatic (EventHandlerUtils .class );
159
- PowerMockito .doThrow (new IOException ()).when (EventHandlerUtils .class );
160
- EventHandlerUtils .decompress (any ()); // PowerMockito throws exception on this static method
166
+ try (MockedStatic <EventHandlerUtils > mockedStatic = mockStatic (EventHandlerUtils .class )) {
167
+ mockedStatic .when (() -> EventHandlerUtils .compress (anyString ())).thenThrow (new IOException ());
161
168
162
- String str = eventWorker .getEventBodyFromInputData (data );
163
- assertNull (str );
169
+ // return original body if compress fails
170
+
171
+ EventHandlerUtils .decompress (any ());
172
+
173
+ String str = eventWorker .getEventBodyFromInputData (data );
174
+ assertNull (str );
175
+
176
+ }
164
177
}
165
178
166
179
@ Test
0 commit comments