Skip to content

Commit 1a93f96

Browse files
Add unit tests for SingleQueryResultCallback, verifying onCompletion and always methods to ensure correct behavior and enhance test coverage.
1 parent 105cd5c commit 1a93f96

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.contentstack.sdk;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class TestSingleQueryResultCallback {
8+
9+
private static class TestSingleQueryCallback extends SingleQueryResultCallback {
10+
11+
ResponseType lastResponseType;
12+
Entry lastEntry;
13+
Error lastError;
14+
boolean onCompletionCalled = false;
15+
boolean alwaysCalled = false;
16+
17+
@Override
18+
public void onCompletion(ResponseType responseType, Entry entry, Error error) {
19+
onCompletionCalled = true;
20+
lastResponseType = responseType;
21+
lastEntry = entry;
22+
lastError = error;
23+
}
24+
25+
@Override
26+
void always() {
27+
alwaysCalled = true;
28+
}
29+
}
30+
31+
@Test
32+
public void testOnRequestFinishCallsOnCompletionWithEntryAndNullError() {
33+
TestSingleQueryCallback callback = new TestSingleQueryCallback();
34+
35+
// Use any valid ResponseType constant from your SDK
36+
ResponseType responseType = ResponseType.NETWORK; // change if needed
37+
38+
// We can't construct Entry, but we only need to verify it's non-null.
39+
// So we'll just pass null here and assert behavior around that.
40+
// To still meaningfully test the path, we just check that:
41+
// - onCompletion is called
42+
// - responseType is passed correctly
43+
// - error is null
44+
callback.onRequestFinish(responseType, null);
45+
46+
assertTrue(callback.onCompletionCalled);
47+
assertEquals(responseType, callback.lastResponseType);
48+
// we passed null, so this should be null
49+
assertNull(callback.lastEntry);
50+
assertNull(callback.lastError);
51+
assertFalse(callback.alwaysCalled);
52+
}
53+
54+
@Test
55+
public void testOnRequestFailCallsOnCompletionWithErrorAndNullEntry() {
56+
TestSingleQueryCallback callback = new TestSingleQueryCallback();
57+
58+
ResponseType responseType = ResponseType.NETWORK; // change if needed
59+
Error error = new Error(); // your SDK Error with no-arg ctor
60+
61+
callback.onRequestFail(responseType, error);
62+
63+
assertTrue(callback.onCompletionCalled);
64+
assertEquals(responseType, callback.lastResponseType);
65+
assertNull(callback.lastEntry);
66+
assertEquals(error, callback.lastError);
67+
assertFalse(callback.alwaysCalled);
68+
}
69+
70+
@Test
71+
public void testAlwaysOverrideIsCallable() {
72+
TestSingleQueryCallback callback = new TestSingleQueryCallback();
73+
74+
callback.always();
75+
76+
assertTrue(callback.alwaysCalled);
77+
}
78+
}

0 commit comments

Comments
 (0)