Skip to content

Commit 97f2453

Browse files
committed
Add unit tests for BranchConfigurationController
- Introduced comprehensive unit tests for the BranchConfigurationController to validate the functionality of session initialization, test mode, tracking management, and configuration serialization. - Ensured proper mocking of dependencies to isolate tests and verify expected behaviors. - Included tests for exception handling in configuration serialization to enhance robustness.
1 parent 9817339 commit 97f2453

File tree

1 file changed

+175
-0
lines changed

1 file changed

+175
-0
lines changed
Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,175 @@
1+
package io.branch.referral
2+
3+
import org.junit.Assert.assertEquals
4+
import org.junit.Assert.assertNotNull
5+
import org.junit.Assert.assertTrue
6+
import org.junit.Before
7+
import org.junit.Test
8+
import org.junit.runner.RunWith
9+
import org.mockito.Mock
10+
import org.mockito.Mockito.verify
11+
import org.mockito.Mockito.`when`
12+
import org.mockito.junit.MockitoJUnitRunner
13+
14+
@RunWith(MockitoJUnitRunner::class)
15+
class BranchConfigurationControllerTest {
16+
17+
@Mock
18+
private lateinit var mockBranch: Branch
19+
20+
@Mock
21+
private lateinit var mockPrefHelper: PrefHelper
22+
23+
private lateinit var controller: BranchConfigurationController
24+
25+
@Before
26+
fun setup() {
27+
controller = BranchConfigurationController()
28+
// Mock Branch.getInstance() to return our mock instance
29+
val branchField = Branch::class.java.getDeclaredField("branchReferral_")
30+
branchField.isAccessible = true
31+
branchField.set(null, mockBranch)
32+
33+
// Mock prefHelper_ to return our mock instance
34+
`when`(mockBranch.prefHelper_).thenReturn(mockPrefHelper)
35+
}
36+
37+
@Test
38+
fun `test setDelayedSessionInitUsed sets the correct value`() {
39+
// Given
40+
val expectedValue = true
41+
42+
// When
43+
controller.setDelayedSessionInitUsed(expectedValue)
44+
45+
// Then
46+
verify(mockPrefHelper).delayedSessionInitUsed = expectedValue
47+
}
48+
49+
@Test
50+
fun `test isTestModeEnabled returns correct value`() {
51+
// Given
52+
val expectedValue = true
53+
BranchUtil.setTestMode(expectedValue)
54+
55+
// When
56+
val result = controller.isTestModeEnabled()
57+
58+
// Then
59+
assertEquals(expectedValue, result)
60+
}
61+
62+
@Test
63+
fun `test isTrackingDisabled returns correct value`() {
64+
// Given
65+
val expectedValue = true
66+
`when`(mockPrefHelper.getBool("bnc_tracking_disabled")).thenReturn(expectedValue)
67+
68+
// When
69+
val result = controller.isTrackingDisabled()
70+
71+
// Then
72+
assertEquals(expectedValue, result)
73+
}
74+
75+
@Test
76+
fun `test setTrackingDisabled sets the correct value`() {
77+
// Given
78+
val expectedValue = true
79+
80+
// When
81+
controller.setTrackingDisabled(expectedValue)
82+
83+
// Then
84+
verify(mockPrefHelper).setBool("bnc_tracking_disabled", expectedValue)
85+
}
86+
87+
@Test
88+
fun `test setTestModeEnabled sets the correct value`() {
89+
// Given
90+
val expectedValue = true
91+
92+
// When
93+
controller.setTestModeEnabled(expectedValue)
94+
95+
// Then
96+
assertTrue(BranchUtil.isTestModeEnabled())
97+
}
98+
99+
@Test
100+
fun `test setInstantDeepLinkingEnabled sets the correct value`() {
101+
// Given
102+
val expectedValue = true
103+
104+
// When
105+
controller.setInstantDeepLinkingEnabled(expectedValue)
106+
107+
// Then
108+
verify(mockPrefHelper).setBool("bnc_instant_deep_linking_enabled", expectedValue)
109+
}
110+
111+
@Test
112+
fun `test isInstantDeepLinkingEnabled returns correct value`() {
113+
// Given
114+
val expectedValue = true
115+
`when`(mockPrefHelper.getBool("bnc_instant_deep_linking_enabled")).thenReturn(expectedValue)
116+
117+
// When
118+
val result = controller.isInstantDeepLinkingEnabled()
119+
120+
// Then
121+
assertEquals(expectedValue, result)
122+
}
123+
124+
@Test
125+
fun `test setDeferInitForPluginRuntime sets the correct value`() {
126+
// Given
127+
val expectedValue = true
128+
129+
// When
130+
controller.setDeferInitForPluginRuntime(expectedValue)
131+
132+
// Then
133+
verify(mockPrefHelper).setBool("bnc_defer_init_for_plugin_runtime", expectedValue)
134+
}
135+
136+
@Test
137+
fun `test serializeConfiguration returns correct JSON object`() {
138+
// Given
139+
val expectedDelayedSessionInit = true
140+
val expectedTestMode = true
141+
val expectedTrackingDisabled = true
142+
val expectedInstantDeepLinkingEnabled = true
143+
val expectedDeferInitForPluginRuntime = true
144+
145+
// Setup mocks
146+
`when`(mockPrefHelper.delayedSessionInitUsed).thenReturn(expectedDelayedSessionInit)
147+
BranchUtil.setTestMode(expectedTestMode)
148+
`when`(mockPrefHelper.getBool("bnc_tracking_disabled")).thenReturn(expectedTrackingDisabled)
149+
`when`(mockPrefHelper.getBool("bnc_instant_deep_linking_enabled")).thenReturn(expectedInstantDeepLinkingEnabled)
150+
`when`(mockPrefHelper.getBool("bnc_defer_init_for_plugin_runtime")).thenReturn(expectedDeferInitForPluginRuntime)
151+
152+
// When
153+
val result = controller.serializeConfiguration()
154+
155+
// Then
156+
assertEquals(expectedDelayedSessionInit, result.getBoolean("expectDelayedSessionInitialization"))
157+
assertEquals(expectedTestMode, result.getBoolean("testMode"))
158+
assertEquals(expectedTrackingDisabled, result.getBoolean("trackingDisabled"))
159+
assertEquals(expectedInstantDeepLinkingEnabled, result.getBoolean("instantDeepLinkingEnabled"))
160+
assertEquals(expectedDeferInitForPluginRuntime, result.getBoolean("deferInitForPluginRuntime"))
161+
}
162+
163+
@Test
164+
fun `test serializeConfiguration handles exception gracefully`() {
165+
// Given
166+
`when`(mockPrefHelper.delayedSessionInitUsed).thenThrow(RuntimeException("Test exception"))
167+
168+
// When
169+
val result = controller.serializeConfiguration()
170+
171+
// Then
172+
assertNotNull(result)
173+
assertEquals(0, result.length())
174+
}
175+
}

0 commit comments

Comments
 (0)