Skip to content

Commit f16c220

Browse files
authored
Fix Util.handleException to rethrow exceptions during unit testing. (#401)
1 parent 85429e5 commit f16c220

File tree

5 files changed

+81
-29
lines changed

5 files changed

+81
-29
lines changed

AndroidSDKTests/src/test/java/com/leanplum/LeanplumActionContextTest.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import static org.junit.Assert.assertNull;
4848
import static org.junit.Assert.assertTrue;
4949
import static org.mockito.Matchers.any;
50+
import static org.mockito.Matchers.eq;
51+
import static org.mockito.Matchers.isNull;
5052
import static org.mockito.Mockito.never;
5153
import static org.mockito.Mockito.times;
5254
import static org.powermock.api.mockito.PowerMockito.doReturn;
@@ -190,7 +192,7 @@ public void testActionArgs() {
190192
}
191193

192194
@Test
193-
public void testValues() {
195+
public void testValues() throws Exception {
194196
ActionContext actionContext = new ActionContext("name", new HashMap<String, Object>() {{
195197
put("1", true);
196198
put("2", false);
@@ -215,6 +217,8 @@ public void testValues() {
215217

216218
assertNotNull(actionContext.numberNamed("7"));
217219
assertNull(actionContext.numberNamed(null));
220+
221+
resumeLeanplumExceptionHandling();
218222
assertEquals(0.0, actionContext.numberNamed("6"));
219223
}
220224

@@ -264,10 +268,26 @@ public void testTrack() {
264268
put("test_2", 10);
265269
}};
266270

267-
verifyStatic(times(1));
271+
Map<String, String> requestArgs = new HashMap<>();
272+
requestArgs.put(Constants.Params.MESSAGE_ID, "messageId");
273+
268274
actionContext.track("test_event", 0.0, map);
269-
verifyStatic(never());
275+
verifyStatic(times(1));
276+
LeanplumInternal.track(
277+
eq("test_event"),
278+
eq(0.0),
279+
isNull(String.class),
280+
eq(map),
281+
eq(requestArgs));
282+
270283
actionContext.track(null, 0.0, map);
284+
verifyStatic(never());
285+
LeanplumInternal.track(
286+
isNull(String.class),
287+
any(Double.class),
288+
any(String.class),
289+
any(Map.class),
290+
any(Map.class));
271291
}
272292

273293
@Test

AndroidSDKTests/src/test/java/com/leanplum/__setup/AbstractTest.java

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,9 @@
3434
import com.leanplum.LocationManager;
3535
import com.leanplum._whitebox.utilities.RequestHelper;
3636
import com.leanplum._whitebox.utilities.ResponseHelper;
37-
import com.leanplum._whitebox.utilities.SynchronousExecutor;
38-
import com.leanplum.callbacks.StartCallback;
3937
import com.leanplum.internal.Constants;
4038
import com.leanplum.internal.LeanplumEventDataManager;
4139
import com.leanplum.internal.LeanplumInternal;
42-
import com.leanplum.internal.Operation;
4340
import com.leanplum.internal.OperationQueue;
4441
import com.leanplum.internal.RequestOld;
4542
import com.leanplum.internal.ShadowOperationQueue;
@@ -50,6 +47,7 @@
5047
import org.junit.Before;
5148
import org.junit.Rule;
5249
import org.junit.runner.RunWith;
50+
import org.powermock.api.mockito.PowerMockito;
5351
import org.powermock.core.classloader.annotations.PowerMockIgnore;
5452
import org.powermock.core.classloader.annotations.PrepareForTest;
5553
import org.powermock.modules.junit4.rule.PowerMockRule;
@@ -66,14 +64,13 @@
6664
import java.util.ArrayList;
6765
import java.util.List;
6866
import java.util.Map;
69-
import java.util.concurrent.CountDownLatch;
70-
import java.util.concurrent.TimeUnit;
7167

7268
import javax.net.ssl.HttpsURLConnection;
7369

7470
import static org.junit.Assert.assertEquals;
7571
import static org.junit.Assert.assertNotNull;
7672
import static org.junit.Assert.assertTrue;
73+
import static org.mockito.Matchers.any;
7774
import static org.mockito.Matchers.anyString;
7875
import static org.powermock.api.mockito.PowerMockito.doReturn;
7976
import static org.powermock.api.mockito.PowerMockito.mock;
@@ -169,13 +166,42 @@ public void before() throws Exception {
169166
when(httpsURLConnection.getInputStream()).thenReturn(ResponseHelper
170167
.seedInputStream("/responses/simple_start_response.json"));
171168

169+
stopLeanplumExceptionHandling();
170+
172171
ShadowOperationQueue shadowOperationQueue = new ShadowOperationQueue();
173172

174173
Field instance = OperationQueue.class.getDeclaredField("instance");
175174
instance.setAccessible(true);
176175
instance.set(instance, shadowOperationQueue);
177176
}
178177

178+
/**
179+
* Leanplum SDK is handling the uncaught exceptions in
180+
* {@link Util#handleException(java.lang.Throwable)} but for test purposes uncaught exceptions
181+
* need not to be caught. In a lot of tests there are assert statements in the callbacks that are
182+
* added in the SDK.
183+
*/
184+
protected void stopLeanplumExceptionHandling() throws Exception {
185+
String message = "\n" + "com.leanplum.internal.Util.handleException(Throwable) is called and "
186+
+ "exception parameter is rethrown intentionally." + "\n"
187+
+ "Call AbstractTest.resumeLeanplumExceptionHandling() to allow "
188+
+ "Util.handleException(Throwable) to work normally." + "\n" + "\n"
189+
+ "Scroll down to see the original stacktrace.";
190+
191+
PowerMockito.doAnswer(invocation -> {
192+
Object[] args = invocation.getArguments();
193+
throw new Exception(message, (Throwable) args[0]);
194+
}).when(Util.class, "handleException", any(Throwable.class));
195+
}
196+
197+
/**
198+
* Use this method to resume normal behaviour for
199+
* {@link Util#handleException(java.lang.Throwable)} and catch all uncaught exceptions in SDK.
200+
*/
201+
protected void resumeLeanplumExceptionHandling() throws Exception {
202+
PowerMockito.doNothing().when(Util.class, "handleException", any(Throwable.class));
203+
}
204+
179205
@After
180206
public void after() {
181207
LeanplumTestHelper.tearDown();

AndroidSDKTests/src/test/java/com/leanplum/__setup/LeanplumTestHelper.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ public static void reset() {
134134
List onceNoDownloadsHandlers = (List) TestClassUtil.getField(Leanplum.class,
135135
"onceNoDownloadsHandlers");
136136
onceNoDownloadsHandlers.clear();
137+
List messageDisplayedHandlers =
138+
(List) TestClassUtil.getField(Leanplum.class, "messageDisplayedHandlers");
139+
messageDisplayedHandlers.clear();
140+
137141
LeanplumInternal.getActionHandlers().clear();
138142
LeanplumInternal.getUserAttributeChanges().clear();
139143
Leanplum.countAggregator().getAndClearCounts();

AndroidSDKTests/src/test/java/com/leanplum/internal/FileManagerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public void testFileDownload() {
7777
FileManager.maybeDownloadFile(false, "test.png", "test_default.png", null, new Runnable() {
7878
@Override
7979
public void run() {
80-
String path = FileManager.fileRelativeToAppBundle("test.png");
80+
String path = FileManager.fileRelativeToDocuments("test.png");
8181
assertTrue(FileManager.fileExistsAtPath(path));
8282
}
8383
});

AndroidSDKTests/src/test/java/com/leanplum/internal/UtilTest.java

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package com.leanplum.internal;
22

3-
import com.leanplum.__setup.LeanplumTestApp;
3+
import com.leanplum.__setup.AbstractTest;
44
import com.leanplum._whitebox.utilities.ResponseHelper;
55

6+
import org.junit.Assert;
67
import org.junit.Before;
78
import org.junit.Test;
8-
import org.junit.runner.RunWith;
9-
import org.powermock.core.classloader.annotations.PowerMockIgnore;
10-
import org.powermock.core.classloader.annotations.PrepareForTest;
11-
import org.robolectric.RobolectricTestRunner;
12-
import org.robolectric.annotation.Config;
139

1410
import java.net.HttpURLConnection;
1511

@@ -22,20 +18,7 @@
2218
*
2319
* @author Hrishi Amravatkar
2420
*/
25-
@RunWith(RobolectricTestRunner.class)
26-
@Config(
27-
sdk = 16,
28-
application = LeanplumTestApp.class
29-
)
30-
@PowerMockIgnore({
31-
"org.mockito.*",
32-
"org.robolectric.*",
33-
"org.json.*",
34-
"org.powermock.*",
35-
"android.*"
36-
})
37-
@PrepareForTest({Util.class})
38-
public class UtilTest {
21+
public class UtilTest extends AbstractTest {
3922

4023
/**
4124
* Runs before every test case.
@@ -81,4 +64,23 @@ public void getGzipEncodedErrorResponseWithContentEndingTest() throws Exception
8164
assertNotNull(Util.getJsonResponse(mockHttpUrlConnection));
8265
}
8366

67+
/**
68+
* Test that {@link Util#handleException(Throwable)} is successfully mocked to rethrow the
69+
* argument exception.
70+
*/
71+
@Test
72+
public void testHandleExceptionMocked() {
73+
Assert.assertThrows(Throwable.class, () -> Util.handleException(new Exception()));
74+
}
75+
76+
/**
77+
* Test that {@link AbstractTest#resumeLeanplumExceptionHandling()} is returning the default
78+
* behaviour of {@link Util#handleException(Throwable)}.
79+
*/
80+
@Test
81+
public void testHandleExceptionDefault() throws Exception {
82+
resumeLeanplumExceptionHandling();
83+
Util.handleException(new Exception());
84+
}
85+
8486
}

0 commit comments

Comments
 (0)