Skip to content

Commit 5bceb99

Browse files
Will-ShaoHuarli
andauthored
test(amazon Q): fix test failure caused by MockTelemetryExtension throwing NotAMockException (#5258)
## Description <!--- Describe your changes in detail --> <!--- If appropriate, providing screenshots will help us review your contribution --> <!--- If there is a related issue, please provide a link here --> Now there are 2 ways to use NoOpTelemetryService 1. if devs are not interested in validating the metric contents, don't need do anything as default test implementation is NoOpTelemetryService 2. if devs are interested in the metric contents, can use `MockTelemetryServiceExtension` which will inject a spy of batcher and replace the original test implementation in junit before hook and revert it in after hook so that you can use ArgumentCaptor to capture what's going through the batcher Relevant #5207 ### root cause: Originally by doing `class NoOpTelemetryService : TelemetryService(publisher, spy(DefaultTelemetryBatcher(publisher))) {`, it's not 100% guaranteed that the TelemetryBatcher passed in is a mockito spy, and it result in `NotAMockException` thrown from these cases. ### solution Manually inject spy batcher separately in a different step instead of IDE doing the work when initializing app/project service components--------- Co-authored-by: Richard Li <[email protected]>
1 parent 892598f commit 5bceb99

File tree

1 file changed

+23
-6
lines changed
  • plugins/core/jetbrains-community/tstFixtures/software/aws/toolkits/jetbrains/services/telemetry

1 file changed

+23
-6
lines changed

plugins/core/jetbrains-community/tstFixtures/software/aws/toolkits/jetbrains/services/telemetry/MockTelemetryService.kt

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33

44
package software.aws.toolkits.jetbrains.services.telemetry
55

6-
import com.intellij.openapi.components.service
6+
import com.intellij.openapi.Disposable
7+
import com.intellij.openapi.application.ApplicationManager
8+
import com.intellij.openapi.util.Disposer
9+
import com.intellij.testFramework.replaceService
710
import org.junit.jupiter.api.extension.AfterEachCallback
811
import org.junit.jupiter.api.extension.BeforeEachCallback
912
import org.junit.jupiter.api.extension.ExtensionContext
@@ -13,13 +16,17 @@ import org.mockito.kotlin.spy
1316
import software.amazon.awssdk.services.toolkittelemetry.model.Sentiment
1417
import software.aws.toolkits.core.telemetry.DefaultTelemetryBatcher
1518
import software.aws.toolkits.core.telemetry.MetricEvent
19+
import software.aws.toolkits.core.telemetry.TelemetryBatcher
1620
import software.aws.toolkits.core.telemetry.TelemetryPublisher
1721

18-
class NoOpTelemetryService : TelemetryService(publisher, spy(DefaultTelemetryBatcher(publisher))) {
22+
class NoOpTelemetryService : TelemetryService {
23+
constructor(noOpPublisher: NoOpPublisher, batcher: TelemetryBatcher) : super(noOpPublisher, batcher)
24+
constructor() : this(NO_OP_PUBLISHER, DefaultTelemetryBatcher(NO_OP_PUBLISHER))
25+
1926
fun batcher() = super.batcher
2027

21-
private companion object {
22-
private val publisher: TelemetryPublisher by lazy { NoOpPublisher() }
28+
companion object {
29+
val NO_OP_PUBLISHER = NoOpPublisher()
2330
}
2431
}
2532

@@ -32,11 +39,21 @@ class NoOpPublisher : TelemetryPublisher {
3239
}
3340

3441
sealed class MockTelemetryServiceBase : ExternalResource() {
35-
private val mockTelemetryService: NoOpTelemetryService
36-
get() = service<TelemetryService>() as NoOpTelemetryService
42+
protected val publisher: NoOpPublisher by lazy { NoOpTelemetryService.NO_OP_PUBLISHER }
43+
protected val batcher: TelemetryBatcher by lazy { spy(DefaultTelemetryBatcher(publisher)) }
44+
private lateinit var disposableParent: Disposable
45+
46+
private val mockTelemetryService: NoOpTelemetryService by lazy { NoOpTelemetryService(publisher, batcher) }
47+
48+
override fun before() {
49+
// hack because @TestDisposable doesn't work here as it's not a test
50+
disposableParent = Disposer.newDisposable()
51+
ApplicationManager.getApplication().replaceService(TelemetryService::class.java, mockTelemetryService, disposableParent)
52+
}
3753

3854
override fun after() {
3955
reset(batcher())
56+
Disposer.dispose(disposableParent)
4057
}
4158

4259
fun telemetryService() = mockTelemetryService

0 commit comments

Comments
 (0)