@@ -27,6 +27,8 @@ import io.sentry.test.DeferredExecutorService
2727import io.sentry.test.getProperty
2828import io.sentry.transport.RateLimiter
2929import org.junit.runner.RunWith
30+ import org.mockito.Mockito
31+ import org.mockito.Mockito.mockStatic
3032import org.mockito.kotlin.any
3133import org.mockito.kotlin.check
3234import org.mockito.kotlin.eq
@@ -59,6 +61,7 @@ class AndroidContinuousProfilerTest {
5961 val buildInfo = mock<BuildInfoProvider > {
6062 whenever(it.sdkInfoVersion).thenReturn(Build .VERSION_CODES .LOLLIPOP_MR1 )
6163 }
64+ val mockedSentry = mockStatic(Sentry ::class .java)
6265 val mockLogger = mock<ILogger >()
6366 val mockTracesSampler = mock<TracesSampler >()
6467
@@ -77,7 +80,7 @@ class AndroidContinuousProfilerTest {
7780 }
7881
7982 init {
80- whenever(mockTracesSampler.sampleSessionProfile()).thenReturn(true )
83+ whenever(mockTracesSampler.sampleSessionProfile(any() )).thenReturn(true )
8184 }
8285
8386 fun getSut (buildInfoProvider : BuildInfoProvider = buildInfo, optionConfig : ((options: SentryAndroidOptions ) -> Unit ) = {}): AndroidContinuousProfiler {
@@ -133,11 +136,14 @@ class AndroidContinuousProfilerTest {
133136 File (fixture.options.profilingTracesDirPath!! ).mkdirs()
134137
135138 Sentry .setCurrentScopes(fixture.scopes)
139+
140+ fixture.mockedSentry.`when `<Any > { Sentry .getCurrentScopes() }.thenReturn(fixture.scopes)
136141 }
137142
138143 @AfterTest
139144 fun clear () {
140145 context.cacheDir.deleteRecursively()
146+ fixture.mockedSentry.close()
141147 }
142148
143149 @Test
@@ -189,7 +195,7 @@ class AndroidContinuousProfilerTest {
189195 @Test
190196 fun `profiler logs a warning on start if not sampled` () {
191197 val profiler = fixture.getSut()
192- whenever(fixture.mockTracesSampler.sampleSessionProfile()).thenReturn(false )
198+ whenever(fixture.mockTracesSampler.sampleSessionProfile(any() )).thenReturn(false )
193199 profiler.startProfileSession(ProfileLifecycle .MANUAL , fixture.mockTracesSampler)
194200 assertFalse(profiler.isRunning)
195201 verify(fixture.mockLogger).log(eq(SentryLevel .DEBUG ), eq(" Profiler was not started due to sampling decision." ))
@@ -198,28 +204,28 @@ class AndroidContinuousProfilerTest {
198204 @Test
199205 fun `profiler evaluates sessionSampleRate only the first time` () {
200206 val profiler = fixture.getSut()
201- verify(fixture.mockTracesSampler, never()).sampleSessionProfile()
207+ verify(fixture.mockTracesSampler, never()).sampleSessionProfile(any() )
202208 // The first time the profiler is started, the sessionSampleRate is evaluated
203209 profiler.startProfileSession(ProfileLifecycle .MANUAL , fixture.mockTracesSampler)
204- verify(fixture.mockTracesSampler, times(1 )).sampleSessionProfile()
210+ verify(fixture.mockTracesSampler, times(1 )).sampleSessionProfile(any() )
205211 // Then, the sessionSampleRate is not evaluated again
206212 profiler.startProfileSession(ProfileLifecycle .MANUAL , fixture.mockTracesSampler)
207- verify(fixture.mockTracesSampler, times(1 )).sampleSessionProfile()
213+ verify(fixture.mockTracesSampler, times(1 )).sampleSessionProfile(any() )
208214 }
209215
210216 @Test
211217 fun `when reevaluateSampling, profiler evaluates sessionSampleRate on next start` () {
212218 val profiler = fixture.getSut()
213- verify(fixture.mockTracesSampler, never()).sampleSessionProfile()
219+ verify(fixture.mockTracesSampler, never()).sampleSessionProfile(any() )
214220 // The first time the profiler is started, the sessionSampleRate is evaluated
215221 profiler.startProfileSession(ProfileLifecycle .MANUAL , fixture.mockTracesSampler)
216- verify(fixture.mockTracesSampler, times(1 )).sampleSessionProfile()
222+ verify(fixture.mockTracesSampler, times(1 )).sampleSessionProfile(any() )
217223 // When reevaluateSampling is called, the sessionSampleRate is not evaluated immediately
218224 profiler.reevaluateSampling()
219- verify(fixture.mockTracesSampler, times(1 )).sampleSessionProfile()
225+ verify(fixture.mockTracesSampler, times(1 )).sampleSessionProfile(any() )
220226 // Then, when the profiler starts again, the sessionSampleRate is reevaluated
221227 profiler.startProfileSession(ProfileLifecycle .MANUAL , fixture.mockTracesSampler)
222- verify(fixture.mockTracesSampler, times(2 )).sampleSessionProfile()
228+ verify(fixture.mockTracesSampler, times(2 )).sampleSessionProfile(any() )
223229 }
224230
225231 @Test
@@ -538,4 +544,9 @@ class AndroidContinuousProfilerTest {
538544 assertEquals(SentryId .EMPTY_ID , profiler.profilerId)
539545 verify(fixture.mockLogger).log(eq(SentryLevel .WARNING ), eq(" Device is offline. Stopping profiler." ))
540546 }
547+
548+ fun withMockScopes (closure : () -> Unit ) = Mockito .mockStatic(Sentry ::class .java).use {
549+ it.`when `<Any > { Sentry .getCurrentScopes() }.thenReturn(fixture.scopes)
550+ closure.invoke()
551+ }
541552}
0 commit comments