1919import static org .junit .Assert .assertEquals ;
2020import static org .junit .Assert .assertFalse ;
2121import 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 ;
2424import static org .mockito .Mockito .mock ;
2525import static org .mockito .Mockito .when ;
26+ import static org .mockito .Mockito .*;
27+
28+ import org .mockito .MockedStatic ;
29+
2630
2731import android .content .Context ;
2832
3539import org .junit .Test ;
3640import org .junit .runner .RunWith ;
3741import 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 ;
4243
4344import java .io .IOException ;
4445
4546/**
4647 * Tests {@link EventWorker}
4748 */
48- @ RunWith (PowerMockRunner .class )
49- @ PrepareForTest ({ EventWorker .class , EventHandlerUtils .class , WorkerParameters .class })
50- @ PowerMockIgnore ("jdk.internal.reflect.*" )
49+ @ RunWith (MockitoJUnitRunner .class )
5150public class EventWorkerUnitTest {
5251
53- private WorkerParameters mockWorkParams = PowerMockito . mock (WorkerParameters .class );
52+ private WorkerParameters mockWorkParams = mock (WorkerParameters .class );
5453 private EventWorker eventWorker = new EventWorker (mock (Context .class ), mockWorkParams );
5554
5655 private String host = "http://www.foo.com" ;
@@ -72,31 +71,40 @@ public void dataForCompressedEvent() {
7271 assertEquals (data .getString ("bodyCompressed" ), base64 );
7372 assertNull (data .getString ("body" ));
7473 }
75-
7674 @ Test
7775 public void compressEvent () throws IOException {
7876 String base64 = "abc123" ;
79- PowerMockito .mockStatic (EventHandlerUtils .class );
80- when (EventHandlerUtils .compress (anyString ())).thenReturn (base64 );
8177
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+ }
8692 }
8793
94+
8895 @ Test
8996 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 ());
9399
94- // return original body if compress fails
100+ // return original body if compress fails
95101
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+ }
100108 }
101109
102110 @ Test
@@ -155,12 +163,17 @@ public void getEventBodyFromInputDataCompressed() {
155163 public void getEventBodyFromInputDataDecompressFailure () throws Exception {
156164 Data data = EventWorker .compressEvent (host , smallBody );
157165
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 ());
161168
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+ }
164177 }
165178
166179 @ Test
0 commit comments