Skip to content

Commit

Permalink
task(SDK-3639) - Refactors unit tests to remove powermock dependency,…
Browse files Browse the repository at this point in the history
… updates mockito version
  • Loading branch information
Anush-Shand committed Jan 31, 2024
1 parent f782daa commit 782d63a
Show file tree
Hide file tree
Showing 9 changed files with 443 additions and 614 deletions.
3 changes: 1 addition & 2 deletions clevertap-geofence/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,14 @@ dependencies {
testImplementation (libs.test.junit.platform.runner)
testImplementation (libs.test.junit.jupiter.api)

testImplementation (libs.test.mockito.core)
testApi (libs.bundles.mockito)
testImplementation (libs.test.robolectric)

testImplementation (libs.test.core)
testImplementation (libs.test.ext.junit)
testImplementation (libs.androidx.appcompat)
testImplementation (libs.firebase.messaging)

testImplementation (libs.bundles.powermock)
testImplementation (libs.catch.exception)
testImplementation (project(":clevertap-core"))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,17 @@
import static org.awaitility.Awaitility.await;

import com.clevertap.android.geofence.interfaces.CTGeofenceTask;
import java.util.concurrent.Callable;
import java.util.concurrent.Future;
import org.junit.*;
import org.junit.runner.*;
import org.mockito.*;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = 28,
application = TestApplication.class
)
public class CTGeofenceTaskManagerTest extends BaseTestCase {


@Before
public void setUp() throws Exception {

MockitoAnnotations.initMocks(this);
MockitoAnnotations.openMocks(this);
super.setUp();
}

Expand All @@ -38,19 +30,9 @@ public void testGetInstance() {
public void testPostAsyncSafelyRunnable() {

final boolean[] isFinish = {false};
Future<?> future = CTGeofenceTaskManager.getInstance().postAsyncSafely("", new Runnable() {
@Override
public void run() {
isFinish[0] = true;
}
});
Future<?> future = CTGeofenceTaskManager.getInstance().postAsyncSafely("", () -> isFinish[0] = true);

await().until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return isFinish[0];
}
});
await().until(() -> isFinish[0]);

Assert.assertNotNull(future);
}
Expand All @@ -62,26 +44,11 @@ public void testPostAsyncSafelyRunnableFlatCall() {

final boolean[] isFinish = {false, false};
final Future<?>[] flatFuture = {null, null};
flatFuture[0] = CTGeofenceTaskManager.getInstance().postAsyncSafely("", new Runnable() {
@Override
public void run() {
isFinish[0] = true;
}
});
flatFuture[0] = CTGeofenceTaskManager.getInstance().postAsyncSafely("", () -> isFinish[0] = true);

flatFuture[1] = CTGeofenceTaskManager.getInstance().postAsyncSafely("nested", new Runnable() {
@Override
public void run() {
isFinish[1] = true;
}
});
flatFuture[1] = CTGeofenceTaskManager.getInstance().postAsyncSafely("nested", () -> isFinish[1] = true);

await().until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return isFinish[0] && isFinish[1];
}
});
await().until(() -> isFinish[0] && isFinish[1]);

Assert.assertNotNull(flatFuture[0]);
Assert.assertNotNull(flatFuture[1]);
Expand All @@ -94,26 +61,11 @@ public void testPostAsyncSafelyRunnableNestedCall() {

final boolean[] isFinish = {false};
final Future<?>[] nestedFuture = {null};
Future<?> future = CTGeofenceTaskManager.getInstance().postAsyncSafely("", new Runnable() {
@Override
public void run() {

nestedFuture[0] = CTGeofenceTaskManager.getInstance().postAsyncSafely("nested", new Runnable() {
@Override
public void run() {
isFinish[0] = true;
}
});

}
});
Future<?> future = CTGeofenceTaskManager.getInstance().postAsyncSafely("",
() -> nestedFuture[0] = CTGeofenceTaskManager.getInstance().postAsyncSafely("nested",
() -> isFinish[0] = true));

await().until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return isFinish[0];
}
});
await().until(() -> isFinish[0]);

Assert.assertNotNull(future);
Assert.assertNull(nestedFuture[0]);
Expand All @@ -135,12 +87,7 @@ public void setOnCompleteListener(OnCompleteListener onCompleteListener) {
}
});

await().until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return isFinish[0];
}
});
await().until(() -> isFinish[0]);

Assert.assertNotNull(future);
}
Expand Down Expand Up @@ -177,12 +124,7 @@ public void setOnCompleteListener(OnCompleteListener onCompleteListener) {
}
});

await().until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return isFinish[0] && isFinish[1];
}
});
await().until(() -> isFinish[0] && isFinish[1]);

Assert.assertNotNull(flatFuture[0]);
Assert.assertNotNull(flatFuture[1]);
Expand Down Expand Up @@ -219,12 +161,7 @@ public void setOnCompleteListener(OnCompleteListener onCompleteListener) {
}
});

await().until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return isFinish[0];
}
});
await().until(() -> isFinish[0]);

Assert.assertNotNull(future);
Assert.assertNull(nestedFuture[0]);
Expand All @@ -241,12 +178,8 @@ public void testPostAsyncSafelyTaskRunnableNestedCall() {
@Override
public void execute() {

nestedFuture[0] = CTGeofenceTaskManager.getInstance().postAsyncSafely("nested", new Runnable() {
@Override
public void run() {
isFinish[0] = true;
}
});
nestedFuture[0] = CTGeofenceTaskManager.getInstance().postAsyncSafely("nested",
() -> isFinish[0] = true);

}

Expand All @@ -256,12 +189,7 @@ public void setOnCompleteListener(OnCompleteListener onCompleteListener) {
}
});

await().until(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
return isFinish[0];
}
});
await().until(() -> isFinish[0]);

Assert.assertNotNull(future);
Assert.assertNull(nestedFuture[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
import static org.powermock.api.mockito.PowerMockito.verifyStatic;
import static org.powermock.api.mockito.PowerMockito.when;

import android.content.Context;
import com.clevertap.android.geofence.fakes.GeofenceJSON;
Expand All @@ -18,23 +16,9 @@
import java.util.List;
import org.json.JSONObject;
import org.junit.*;
import org.junit.runner.*;
import org.mockito.*;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.rule.PowerMockRule;
import org.powermock.reflect.internal.WhiteboxImpl;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.skyscreamer.jsonassert.JSONAssert;

@RunWith(RobolectricTestRunner.class)
@Config(sdk = 28,
application = TestApplication.class
)
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*", "androidx.*", "org.json.*"})
@PrepareForTest({CTGeofenceAPI.class, FileUtils.class})
public class GeofenceUpdateTaskTest extends BaseTestCase {

@Mock
Expand All @@ -43,11 +27,19 @@ public class GeofenceUpdateTaskTest extends BaseTestCase {
@Mock
public CTGeofenceAdapter ctGeofenceAdapter;

@Rule
public PowerMockRule rule = new PowerMockRule();
private MockedStatic<CTGeofenceAPI> ctGeofenceAPIMockedStatic;

private MockedStatic<FileUtils> fileUtilsMockedStatic;

@Mock
private Logger logger;

@After
public void cleanup() {
ctGeofenceAPIMockedStatic.close();
fileUtilsMockedStatic.close();
}

@Test
public void executeTestTC1() throws Exception {

Expand All @@ -70,8 +62,8 @@ public void executeTestTC1() throws Exception {

ArgumentCaptor<JSONObject> argumentCaptorJson = ArgumentCaptor.forClass(JSONObject.class);

verifyStatic(FileUtils.class);
FileUtils.writeJsonToFile(any(Context.class), anyString(), anyString(), argumentCaptorJson.capture());
fileUtilsMockedStatic.verify(() -> FileUtils.writeJsonToFile(any(Context.class), anyString(), anyString(),
argumentCaptorJson.capture()));

JSONAssert.assertEquals(GeofenceJSON.getFirst(), argumentCaptorJson.getValue(), true);

Expand Down Expand Up @@ -103,8 +95,8 @@ public void executeTestTC2() throws Exception {

ArgumentCaptor<JSONObject> argumentCaptorJson = ArgumentCaptor.forClass(JSONObject.class);

verifyStatic(FileUtils.class);
FileUtils.writeJsonToFile(any(Context.class), anyString(), anyString(), argumentCaptorJson.capture());
fileUtilsMockedStatic.verify(() -> FileUtils.writeJsonToFile(any(Context.class), anyString(), anyString(),
argumentCaptorJson.capture()));

JSONAssert.assertEquals(GeofenceJSON.getEmptyGeofence(), argumentCaptorJson.getValue(), true);

Expand Down Expand Up @@ -136,8 +128,8 @@ public void executeTestTC3() throws Exception {

ArgumentCaptor<JSONObject> argumentCaptorJson = ArgumentCaptor.forClass(JSONObject.class);

verifyStatic(FileUtils.class);
FileUtils.writeJsonToFile(any(Context.class), anyString(), anyString(), argumentCaptorJson.capture());
fileUtilsMockedStatic.verify(() -> FileUtils.writeJsonToFile(any(Context.class), anyString(), anyString(),
argumentCaptorJson.capture()));

JSONAssert.assertEquals(GeofenceJSON.getEmptyJson(), argumentCaptorJson.getValue(), true);

Expand All @@ -148,7 +140,7 @@ public void executeTestTC3() throws Exception {
}

@Test
public void executeTestTC4() throws Exception {
public void executeTestTC4() {

// when old geofence is not empty and new geofence list is not empty

Expand All @@ -171,7 +163,7 @@ public void executeTestTC4() throws Exception {

verify(ctGeofenceAdapter)
.removeAllGeofence(argumentCaptorOldGeofence.capture(), any(OnSuccessListener.class));
assertThat(argumentCaptorOldGeofence.getValue(), is(Arrays.asList(new String[]{"310001"})));
assertThat(argumentCaptorOldGeofence.getValue(), is(Arrays.asList("310001")));

}

Expand All @@ -197,9 +189,8 @@ public void executeTestTC5() throws Exception {

ArgumentCaptor<JSONObject> argumentCaptorJson = ArgumentCaptor.forClass(JSONObject.class);

verifyStatic(FileUtils.class);
FileUtils.writeJsonToFile(any(Context.class), anyString(), anyString(), argumentCaptorJson.capture());

fileUtilsMockedStatic.verify(() -> FileUtils.writeJsonToFile(any(Context.class), anyString(), anyString(),
argumentCaptorJson.capture()));
JSONAssert.assertEquals(GeofenceJSON.getGeofence(), argumentCaptorJson.getValue(), true);

ArgumentCaptor<List<CTGeofence>> argumentCaptor = ArgumentCaptor.forClass(List.class);
Expand All @@ -210,18 +201,12 @@ public void executeTestTC5() throws Exception {

@Before
public void setUp() throws Exception {

MockitoAnnotations.initMocks(this);
PowerMockito.mockStatic(CTGeofenceAPI.class, FileUtils.class);

MockitoAnnotations.openMocks(this);
super.setUp();

ctGeofenceAPIMockedStatic = Mockito.mockStatic(CTGeofenceAPI.class);
fileUtilsMockedStatic = Mockito.mockStatic(FileUtils.class);
when(CTGeofenceAPI.getInstance(application)).thenReturn(ctGeofenceAPI);
logger = new Logger(Logger.DEBUG);
when(CTGeofenceAPI.getLogger()).thenReturn(logger);

WhiteboxImpl.setInternalState(ctGeofenceAPI, "ctGeofenceAdapter", ctGeofenceAdapter);

when(ctGeofenceAPI.getCtGeofenceAdapter()).thenReturn(ctGeofenceAdapter);
}

}
Loading

0 comments on commit 782d63a

Please sign in to comment.