Skip to content

Commit 6d60bde

Browse files
committed
fix: adjust unit tests
1 parent fee9503 commit 6d60bde

File tree

1 file changed

+25
-10
lines changed

1 file changed

+25
-10
lines changed

Branch-SDK/src/test/java/io/branch/referral/BranchConfigurationControllerTest.kt

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ package io.branch.referral
22

33
import org.junit.Assert.assertEquals
44
import org.junit.Assert.assertNotNull
5-
import org.junit.Assert.assertTrue
65
import org.junit.Before
76
import org.junit.Test
87
import org.junit.runner.RunWith
98
import org.mockito.Mock
9+
import org.mockito.MockedStatic
10+
import org.mockito.Mockito
1011
import org.mockito.Mockito.verify
1112
import org.mockito.Mockito.`when`
1213
import org.mockito.junit.MockitoJUnitRunner
@@ -21,17 +22,24 @@ class BranchConfigurationControllerTest {
2122
private lateinit var mockPrefHelper: PrefHelper
2223

2324
private lateinit var controller: BranchConfigurationController
25+
private lateinit var mockedStaticBranch: MockedStatic<Branch>
26+
private lateinit var mockedStaticBranchUtil: MockedStatic<BranchUtil>
2427

2528
@Before
2629
fun setup() {
2730
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)
3231

33-
// Mock prefHelper_ to return our mock instance
34-
`when`(mockBranch.prefHelper_).thenReturn(mockPrefHelper)
32+
// Set up static mocking
33+
mockedStaticBranch = Mockito.mockStatic(Branch::class.java)
34+
mockedStaticBranchUtil = Mockito.mockStatic(BranchUtil::class.java)
35+
36+
// Mock Branch.getInstance() to return our mock
37+
mockedStaticBranch.`when`<Branch> { Branch.getInstance() }.thenReturn(mockBranch)
38+
39+
// Use reflection to set the prefHelper_ field
40+
val prefHelperField = Branch::class.java.getDeclaredField("prefHelper_")
41+
prefHelperField.isAccessible = true
42+
prefHelperField.set(mockBranch, mockPrefHelper)
3543
}
3644

3745
@Test
@@ -50,7 +58,7 @@ class BranchConfigurationControllerTest {
5058
fun `test isTestModeEnabled returns correct value`() {
5159
// Given
5260
val expectedValue = true
53-
BranchUtil.setTestMode(expectedValue)
61+
mockedStaticBranchUtil.`when`<Boolean> { BranchUtil.isTestModeEnabled() }.thenReturn(expectedValue)
5462

5563
// When
5664
val result = controller.isTestModeEnabled()
@@ -93,7 +101,7 @@ class BranchConfigurationControllerTest {
93101
controller.setTestModeEnabled(expectedValue)
94102

95103
// Then
96-
assertTrue(BranchUtil.isTestModeEnabled())
104+
mockedStaticBranchUtil.verify { BranchUtil.setTestMode(expectedValue) }
97105
}
98106

99107
@Test
@@ -144,7 +152,7 @@ class BranchConfigurationControllerTest {
144152

145153
// Setup mocks
146154
`when`(mockPrefHelper.delayedSessionInitUsed).thenReturn(expectedDelayedSessionInit)
147-
BranchUtil.setTestMode(expectedTestMode)
155+
mockedStaticBranchUtil.`when`<Boolean> { BranchUtil.isTestModeEnabled() }.thenReturn(expectedTestMode)
148156
`when`(mockPrefHelper.getBool("bnc_tracking_disabled")).thenReturn(expectedTrackingDisabled)
149157
`when`(mockPrefHelper.getBool("bnc_instant_deep_linking_enabled")).thenReturn(expectedInstantDeepLinkingEnabled)
150158
`when`(mockPrefHelper.getBool("bnc_defer_init_for_plugin_runtime")).thenReturn(expectedDeferInitForPluginRuntime)
@@ -172,4 +180,11 @@ class BranchConfigurationControllerTest {
172180
assertNotNull(result)
173181
assertEquals(0, result.length())
174182
}
183+
184+
@org.junit.After
185+
fun tearDown() {
186+
// Clean up static mocks
187+
mockedStaticBranch.close()
188+
mockedStaticBranchUtil.close()
189+
}
175190
}

0 commit comments

Comments
 (0)