diff --git a/sdk-java/build.gradle b/sdk-java/build.gradle index 76790b55..a7db0009 100644 --- a/sdk-java/build.gradle +++ b/sdk-java/build.gradle @@ -26,9 +26,7 @@ dependencies { implementation 'com.google.code.findbugs:jsr305:3.0.2' testImplementation 'junit:junit:4.13.1' - testImplementation 'org.mockito:mockito-core:2.8.9' - testImplementation "org.powermock:powermock-core:${POWERMOCK_VERSION}" - testImplementation "org.powermock:powermock-module-junit4:${POWERMOCK_VERSION}" + testImplementation 'org.mockito:mockito-core:4.11.0' //testImplementation 'com.squareup.okhttp3:mockwebserver:3.7.0' } diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/LogTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/LogTests.java index fd1f0b3d..9d7c95a4 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/LogTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/LogTests.java @@ -6,8 +6,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import org.powermock.reflect.Whitebox; - import ly.count.sdk.java.Config; import static ly.count.sdk.java.Config.LoggingLevel.DEBUG; diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/RequestTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/RequestTests.java index 4a7c8b26..5535dd27 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/RequestTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/RequestTests.java @@ -1,12 +1,12 @@ package ly.count.sdk.java.internal; +import java.lang.reflect.Field; import java.net.URL; import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import org.powermock.reflect.Whitebox; @RunWith(JUnit4.class) public class RequestTests { @@ -18,31 +18,36 @@ public void setupEveryTest() throws Exception { url = new URL(urlString); } + private static String getEOR() throws Exception { + Field field = Request.class.getDeclaredField("EOR"); + field.setAccessible(true); + return (String) field.get(null); + } + @Test - public void request_constructorString() throws Exception { + public void request_constructorString() { String paramVals = "a=1&b=2"; Params params = new Params(paramVals); - Request request = Whitebox.invokeConstructor(Request.class, paramVals); + Request request = new Request(paramVals); Params requestParams = request.params; Assert.assertEquals(params.toString(), requestParams.toString()); } @Test - public void request_constructorObjectsNull() throws Exception { + public void request_constructorObjectsNull() { String[] paramsVals = new String[] { "asd", "123" }; - Object[] vals = new Object[] { new Object[] { paramsVals[0], paramsVals[1] } }; - Request request = Whitebox.invokeConstructor(Request.class, vals); + Request request = new Request((Object[]) new Object[] { paramsVals[0], paramsVals[1] }); Assert.assertEquals(paramsVals[0] + "=" + paramsVals[1], request.params.toString()); } @Test - public void request_constructorObjects() throws Exception { + public void request_constructorObjects() { String[] paramsParts = new String[] { "abc", "123", "qwe", "456" }; String paramVals = paramsParts[0] + "=" + paramsParts[1] + "&" + paramsParts[2] + "=" + paramsParts[3]; Params params = new Params(paramVals); - Request request = Whitebox.invokeConstructor(Request.class, paramsParts[0], paramsParts[1], paramsParts[2], paramsParts[3]); + Request request = new Request(paramsParts[0], paramsParts[1], paramsParts[2], paramsParts[3]); Params requestParams = request.params; Assert.assertEquals(params.toString(), requestParams.toString()); } @@ -61,17 +66,17 @@ public void request_build() { @Test public void request_serialize() throws Exception { String paramVals = "a=1&b=2"; - Request request = Whitebox.invokeConstructor(Request.class, paramVals); + Request request = new Request(paramVals); - String manualSerialization = paramVals + Whitebox.getInternalState(Request.class, "EOR"); + String manualSerialization = paramVals + getEOR(); String serializationRes = new String(request.store(null)); Assert.assertEquals(manualSerialization, serializationRes); } @Test - public void request_loadSimple() throws Exception { + public void request_loadSimple() { String paramVals = "a=1&b=2"; - Request request = Whitebox.invokeConstructor(Request.class, paramVals); + Request request = new Request(paramVals); byte[] serializationRes = request.store(null); Request requestNew = new Request(); @@ -92,13 +97,13 @@ public void request_loadNull() { } @Test - public void isGettable_ParamsEmptyUnderLimit() throws Exception { - Request request = Whitebox.invokeConstructor(Request.class, ""); + public void isGettable_ParamsEmptyUnderLimit() { + Request request = new Request(""); Assert.assertTrue(request.isGettable(url, 0)); } @Test - public void isGettable_ParamsFilledAboveLimitLarge() throws Exception { + public void isGettable_ParamsFilledAboveLimitLarge() { StringBuilder sbParams = new StringBuilder(); for (int a = 0; a < 1000; a++) { @@ -109,8 +114,8 @@ public void isGettable_ParamsFilledAboveLimitLarge() throws Exception { sbParams.append('=').append(a); } - Request request = Whitebox.invokeConstructor(Request.class, sbParams.toString()); + Request request = new Request(sbParams.toString()); Assert.assertFalse(request.isGettable(url, 0)); } -} \ No newline at end of file +} diff --git a/sdk-java/src/test/java/ly/count/sdk/java/internal/TasksTests.java b/sdk-java/src/test/java/ly/count/sdk/java/internal/TasksTests.java index bcbce23f..6cbf8825 100644 --- a/sdk-java/src/test/java/ly/count/sdk/java/internal/TasksTests.java +++ b/sdk-java/src/test/java/ly/count/sdk/java/internal/TasksTests.java @@ -1,5 +1,6 @@ package ly.count.sdk.java.internal; +import java.lang.reflect.Field; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; import org.junit.After; @@ -8,7 +9,6 @@ import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.JUnit4; -import org.powermock.reflect.Whitebox; @RunWith(JUnit4.class) public class TasksTests { @@ -24,14 +24,20 @@ public void tearDown() throws Exception { tasks.shutdown(); } + private static Object getField(Object target, String fieldName) throws Exception { + Field field = target.getClass().getDeclaredField(fieldName); + field.setAccessible(true); + return field.get(target); + } + @Test - public void testSetup() { - Assert.assertNotNull(Whitebox.getInternalState(tasks, "executor")); - Assert.assertNotNull(Whitebox.getInternalState(tasks, "pending")); + public void testSetup() throws Exception { + Assert.assertNotNull(getField(tasks, "executor")); + Assert.assertNotNull(getField(tasks, "pending")); } @Test - public void testShutdown() { + public void testShutdown() throws Exception { Tasks other = new Tasks("test", null); other.run(new Tasks.Task(0L) { @Override @@ -43,8 +49,8 @@ public Object call() throws Exception { long now = System.nanoTime(); other.shutdown(); long timeToShutdown = TimeUtils.nsToMs(System.nanoTime() - now); - Assert.assertTrue(Whitebox.getInternalState(other, "executor").isShutdown()); - Assert.assertTrue(Whitebox.getInternalState(other, "executor").isTerminated()); + Assert.assertTrue(((ExecutorService) getField(other, "executor")).isShutdown()); + Assert.assertTrue(((ExecutorService) getField(other, "executor")).isTerminated()); //Assert.assertTrue(timeToShutdown > 100);//todo, this line fails when trying to publish (AK, 12.12.18) }