Skip to content

Commit e5276cb

Browse files
Add unit tests for GlobalFieldsResultCallback, verifying onCompletion and always methods to ensure correct behavior and enhance test coverage.
1 parent 9cbdcf8 commit e5276cb

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.contentstack.sdk;
2+
3+
import org.junit.Test;
4+
5+
import static org.junit.Assert.*;
6+
7+
public class TestGlobalFieldsResultCallback {
8+
9+
private static class TestGlobalFieldsCallback extends GlobalFieldsResultCallback {
10+
11+
GlobalFieldsModel lastModel;
12+
Error lastError;
13+
boolean onCompletionCalled = false;
14+
boolean alwaysCalled = false;
15+
16+
@Override
17+
public void onCompletion(GlobalFieldsModel globalFieldsModel, Error error) {
18+
onCompletionCalled = true;
19+
lastModel = globalFieldsModel;
20+
lastError = error;
21+
}
22+
23+
@Override
24+
void always() {
25+
alwaysCalled = true;
26+
}
27+
}
28+
29+
@Test
30+
public void testOnRequestFinishCallsOnCompletionWithModelAndNullError() {
31+
TestGlobalFieldsCallback callback = new TestGlobalFieldsCallback();
32+
33+
// we don't need a real GlobalFieldsModel, just a non-null reference;
34+
// but if it's hard to construct, we can pass null and test behavior.
35+
GlobalFieldsModel model = null;
36+
37+
callback.onRequestFinish(model);
38+
39+
assertTrue(callback.onCompletionCalled);
40+
assertEquals(model, callback.lastModel);
41+
assertNull(callback.lastError);
42+
assertFalse(callback.alwaysCalled);
43+
}
44+
45+
@Test
46+
public void testOnRequestFailCallsOnCompletionWithErrorAndNullModel() {
47+
TestGlobalFieldsCallback callback = new TestGlobalFieldsCallback();
48+
49+
Error error = new Error(); // your SDK Error (no-arg ctor)
50+
51+
callback.onRequestFail(ResponseType.NETWORK, error); // use any valid ResponseType
52+
53+
assertTrue(callback.onCompletionCalled);
54+
assertNull(callback.lastModel);
55+
assertEquals(error, callback.lastError);
56+
assertFalse(callback.alwaysCalled);
57+
}
58+
59+
@Test
60+
public void testAlwaysOverrideIsCallable() {
61+
TestGlobalFieldsCallback callback = new TestGlobalFieldsCallback();
62+
63+
callback.always();
64+
65+
assertTrue(callback.alwaysCalled);
66+
}
67+
}

0 commit comments

Comments
 (0)