Skip to content

Commit 252d926

Browse files
Add unit tests for ContentstackResultCallback, verifying onCompletion and always methods to ensure correct behavior and high test coverage.
1 parent 451d495 commit 252d926

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package com.contentstack.sdk;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class ContentstackResultCallbackTest {
8+
9+
// Simple concrete implementation for testing
10+
private static class TestCallback extends ContentstackResultCallback {
11+
12+
ResponseType lastResponseType;
13+
Error lastError;
14+
boolean onCompletionCalled = false;
15+
boolean alwaysCalled = false;
16+
17+
@Override
18+
public void onCompletion(ResponseType responseType, Error error) {
19+
onCompletionCalled = true;
20+
lastResponseType = responseType;
21+
lastError = error;
22+
}
23+
24+
@Override
25+
void always() {
26+
alwaysCalled = true;
27+
}
28+
}
29+
30+
@Test
31+
public void testOnRequestFinishCallsOnCompletionWithNullError() {
32+
TestCallback callback = new TestCallback();
33+
34+
// Use any valid ResponseType constant that exists in your SDK
35+
// If NETWORK doesn't exist, replace with SUCCESS or any other valid one.
36+
ResponseType responseType = ResponseType.NETWORK;
37+
38+
callback.onRequestFinish(responseType);
39+
40+
assertTrue(callback.onCompletionCalled);
41+
assertEquals(responseType, callback.lastResponseType);
42+
assertNull(callback.lastError);
43+
assertFalse(callback.alwaysCalled);
44+
}
45+
46+
@Test
47+
public void testOnRequestFailCallsOnCompletionWithError() {
48+
TestCallback callback = new TestCallback();
49+
ResponseType responseType = ResponseType.NETWORK;
50+
51+
// IMPORTANT: this uses the no-arg constructor of your SDK Error class
52+
Error error = new Error();
53+
54+
callback.onRequestFail(responseType, error);
55+
56+
assertTrue(callback.onCompletionCalled);
57+
assertEquals(responseType, callback.lastResponseType);
58+
assertEquals(error, callback.lastError);
59+
assertFalse(callback.alwaysCalled);
60+
}
61+
62+
@Test
63+
public void testAlwaysOverrideIsCallable() {
64+
TestCallback callback = new TestCallback();
65+
66+
callback.always();
67+
68+
assertTrue(callback.alwaysCalled);
69+
}
70+
}

0 commit comments

Comments
 (0)