Skip to content

Commit 35d3888

Browse files
author
Jonathan Pearlin
committed
chore: replace mockito with mockk (part 2) (#18368)
1 parent c72c3ad commit 35d3888

File tree

69 files changed

+3033
-3707
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

69 files changed

+3033
-3707
lines changed

airbyte-commons-temporal-core/build.gradle.kts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ dependencies {
2222
testImplementation(libs.bundles.junit)
2323
testImplementation(libs.junit.pioneer)
2424
testImplementation(libs.mockk)
25-
testImplementation(libs.mockito.inline)
26-
testImplementation(libs.mockito.kotlin)
2725
testImplementation(libs.temporal.testing)
2826
// this is temporarily needed because temporal.testing uses InProcessBuilder which is included in on grpc-core 1.52.2
2927
// featureflag depends on micronaut-platform 4.4.1 depends on grpc-core 1.62.2, which doesn't contain the InProcessBuilder class.

airbyte-commons-temporal-core/src/test/kotlin/io/airbyte/commons/temporal/WorkflowClientWrappedTest.kt

Lines changed: 29 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ package io.airbyte.commons.temporal
77
import io.airbyte.metrics.MetricClient
88
import io.grpc.Status
99
import io.grpc.StatusRuntimeException
10+
import io.mockk.every
11+
import io.mockk.mockk
12+
import io.mockk.verify
1013
import io.temporal.api.workflowservice.v1.DescribeWorkflowExecutionRequest
1114
import io.temporal.api.workflowservice.v1.DescribeWorkflowExecutionResponse
1215
import io.temporal.api.workflowservice.v1.WorkflowServiceGrpc.WorkflowServiceBlockingStub
@@ -17,53 +20,43 @@ import io.temporal.serviceclient.WorkflowServiceStubs
1720
import org.junit.jupiter.api.Assertions
1821
import org.junit.jupiter.api.BeforeEach
1922
import org.junit.jupiter.api.Test
20-
import org.mockito.ArgumentMatchers
21-
import org.mockito.Mockito
22-
import org.mockito.kotlin.anyOrNull
23-
import org.mockito.kotlin.anyVararg
2423

2524
internal class WorkflowClientWrappedTest {
2625
internal class MyWorkflow
2726

28-
lateinit var metricClient: MetricClient
29-
lateinit var temporalWorkflowServiceStubs: WorkflowServiceStubs
30-
lateinit var temporalWorkflowServiceBlockingStub: WorkflowServiceBlockingStub
31-
lateinit var temporalWorkflowClient: WorkflowClient
32-
lateinit var workflowClient: WorkflowClientWrapped
27+
private lateinit var metricClient: MetricClient
28+
private lateinit var temporalWorkflowServiceStubs: WorkflowServiceStubs
29+
private lateinit var temporalWorkflowServiceBlockingStub: WorkflowServiceBlockingStub
30+
private lateinit var temporalWorkflowClient: WorkflowClient
31+
private lateinit var workflowClient: WorkflowClientWrapped
3332

3433
@BeforeEach
3534
fun beforeEach() {
36-
metricClient = Mockito.mock(MetricClient::class.java)
37-
temporalWorkflowServiceBlockingStub = Mockito.mock(WorkflowServiceBlockingStub::class.java)
38-
temporalWorkflowServiceStubs = Mockito.mock(WorkflowServiceStubs::class.java)
39-
Mockito.`when`(temporalWorkflowServiceStubs.blockingStub()).thenReturn(temporalWorkflowServiceBlockingStub)
40-
temporalWorkflowClient = Mockito.mock(WorkflowClient::class.java)
41-
Mockito.`when`(temporalWorkflowClient.getWorkflowServiceStubs()).thenReturn(temporalWorkflowServiceStubs)
35+
metricClient = mockk(relaxed = true)
36+
temporalWorkflowServiceBlockingStub = mockk()
37+
temporalWorkflowServiceStubs = mockk()
38+
every { temporalWorkflowServiceStubs.blockingStub() } returns temporalWorkflowServiceBlockingStub
39+
temporalWorkflowClient = mockk()
40+
every { temporalWorkflowClient.workflowServiceStubs } returns temporalWorkflowServiceStubs
4241
workflowClient =
4342
WorkflowClientWrapped(temporalWorkflowClient, metricClient, MAX_ATTEMPT, BACKOFF_DELAY_IN_MILLIS, BACKOFF_MAX_DELAY_IN_MILLIS)
4443
}
4544

4645
@Test
4746
fun testRetryLogic() {
48-
Mockito.`when`(temporalWorkflowClient.newWorkflowStub(ArgumentMatchers.any<Class<Any>>(), ArgumentMatchers.anyString())).thenAnswer {
49-
throw
50-
unavailable()
51-
}
47+
every { temporalWorkflowClient.newWorkflowStub(any<Class<Any>>(), any<String>()) } throws unavailable()
5248

5349
Assertions.assertThrows(
5450
StatusRuntimeException::class.java,
5551
) { workflowClient.newWorkflowStub(MyWorkflow::class.java, "fail") }
56-
Mockito.verify(temporalWorkflowClient, Mockito.times(3)).newWorkflowStub(ArgumentMatchers.any<Class<Any>>(), ArgumentMatchers.anyString())
57-
Mockito.verify(metricClient, Mockito.times(2)).count(anyOrNull(), anyOrNull(), anyVararg())
52+
verify(exactly = 3) { temporalWorkflowClient.newWorkflowStub(any<Class<Any>>(), any<String>()) }
53+
verify(exactly = 2) { metricClient.count(any(), any(), *anyVararg()) }
5854
}
5955

6056
@Test
6157
fun testNewWorkflowStub() {
6258
val expected = MyWorkflow()
63-
Mockito
64-
.`when`(temporalWorkflowClient.newWorkflowStub(ArgumentMatchers.any<Class<Any>>(), ArgumentMatchers.anyString()))
65-
.thenAnswer { throw unavailable() }
66-
.thenReturn(expected)
59+
every { temporalWorkflowClient.newWorkflowStub(any<Class<Any>>(), any<String>()) } throws unavailable() andThen expected
6760

6861
val actual = workflowClient.newWorkflowStub(MyWorkflow::class.java, "woot")
6962
Assertions.assertEquals(expected, actual)
@@ -72,54 +65,43 @@ internal class WorkflowClientWrappedTest {
7265
@Test
7366
fun testNewWorkflowStubWithOptions() {
7467
val expected = MyWorkflow()
75-
Mockito
76-
.`when`(temporalWorkflowClient.newWorkflowStub(anyOrNull<Class<Any>>(), anyOrNull<WorkflowOptions>()))
77-
.thenAnswer { throw unavailable() }
78-
.thenReturn(expected)
68+
every { temporalWorkflowClient.newWorkflowStub(any<Class<Any>>(), any<WorkflowOptions>()) } throws unavailable() andThen expected
7969

8070
val actual = workflowClient.newWorkflowStub(MyWorkflow::class.java, WorkflowOptions.getDefaultInstance())
8171
Assertions.assertEquals(expected, actual)
8272
}
8373

8474
@Test
8575
fun testTerminateWorkflow() {
86-
val workflowStub = Mockito.mock(WorkflowStub::class.java)
87-
Mockito
88-
.`when`(temporalWorkflowClient.newUntypedWorkflowStub(ArgumentMatchers.anyString()))
89-
.thenAnswer { throw unavailable() }
90-
.thenReturn(workflowStub)
76+
val workflowStub = mockk<WorkflowStub>(relaxed = true)
77+
every { temporalWorkflowClient.newUntypedWorkflowStub(any<String>()) } throws unavailable() andThen workflowStub
9178

9279
workflowClient.terminateWorkflow("workflow", "test terminate")
93-
Mockito.verify(temporalWorkflowClient, Mockito.times(2)).newUntypedWorkflowStub("workflow")
94-
Mockito.verify(workflowStub).terminate("test terminate")
80+
verify(exactly = 2) { temporalWorkflowClient.newUntypedWorkflowStub("workflow") }
81+
verify { workflowStub.terminate("test terminate") }
9582
}
9683

9784
@Test
9885
fun testBlockingDescribeWorkflowExecution() {
99-
val expected = Mockito.mock(DescribeWorkflowExecutionResponse::class.java)
100-
Mockito
101-
.`when`(temporalWorkflowServiceBlockingStub.describeWorkflowExecution(ArgumentMatchers.any()))
102-
.thenAnswer { throw unavailable() }
103-
.thenReturn(expected)
86+
val expected = mockk<DescribeWorkflowExecutionResponse>()
87+
every { temporalWorkflowServiceBlockingStub.describeWorkflowExecution(any()) } throws unavailable() andThen expected
10488

10589
val actual =
10690
workflowClient.blockingDescribeWorkflowExecution(
107-
Mockito.mock(
108-
DescribeWorkflowExecutionRequest::class.java,
109-
),
91+
mockk<DescribeWorkflowExecutionRequest>(),
11092
)
11193
Assertions.assertEquals(expected, actual)
11294
}
11395

11496
@Test
11597
fun testSignalsAreNotRetried() {
116-
Mockito.`when`(temporalWorkflowClient.signalWithStart(ArgumentMatchers.any())).thenAnswer { throw unavailable() }
117-
Mockito.`when`(temporalWorkflowClient.newSignalWithStartRequest()).thenReturn(Mockito.mock())
98+
every { temporalWorkflowClient.signalWithStart(any()) } throws unavailable()
99+
every { temporalWorkflowClient.newSignalWithStartRequest() } returns mockk()
118100
Assertions.assertThrows(StatusRuntimeException::class.java) {
119101
val request = workflowClient.newSignalWithStartRequest()
120102
workflowClient.signalWithStart(request)
121103
}
122-
Mockito.verify(temporalWorkflowClient, Mockito.times(1)).signalWithStart(ArgumentMatchers.any())
104+
verify(exactly = 1) { temporalWorkflowClient.signalWithStart(any()) }
123105
}
124106

125107
companion object {

airbyte-commons-temporal-core/src/test/kotlin/io/airbyte/commons/temporal/WorkflowServiceStubsWrappedTest.kt

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ package io.airbyte.commons.temporal
77
import io.airbyte.metrics.MetricClient
88
import io.grpc.Status
99
import io.grpc.StatusRuntimeException
10+
import io.mockk.every
11+
import io.mockk.mockk
1012
import io.temporal.api.workflowservice.v1.ListClosedWorkflowExecutionsRequest
1113
import io.temporal.api.workflowservice.v1.ListClosedWorkflowExecutionsResponse
1214
import io.temporal.api.workflowservice.v1.ListOpenWorkflowExecutionsRequest
@@ -16,20 +18,19 @@ import io.temporal.serviceclient.WorkflowServiceStubs
1618
import org.junit.jupiter.api.Assertions
1719
import org.junit.jupiter.api.BeforeEach
1820
import org.junit.jupiter.api.Test
19-
import org.mockito.Mockito
2021

2122
internal class WorkflowServiceStubsWrappedTest {
22-
lateinit var metricClient: MetricClient
23-
lateinit var temporalWorkflowServiceStubs: WorkflowServiceStubs
24-
lateinit var temporalWorkflowServiceBlockingStub: WorkflowServiceBlockingStub
25-
lateinit var serviceStubsWrapped: WorkflowServiceStubsWrapped
23+
private lateinit var metricClient: MetricClient
24+
private lateinit var temporalWorkflowServiceStubs: WorkflowServiceStubs
25+
private lateinit var temporalWorkflowServiceBlockingStub: WorkflowServiceBlockingStub
26+
private lateinit var serviceStubsWrapped: WorkflowServiceStubsWrapped
2627

2728
@BeforeEach
2829
fun beforeEach() {
29-
metricClient = Mockito.mock(MetricClient::class.java)
30-
temporalWorkflowServiceBlockingStub = Mockito.mock(WorkflowServiceBlockingStub::class.java)
31-
temporalWorkflowServiceStubs = Mockito.mock(WorkflowServiceStubs::class.java)
32-
Mockito.`when`(temporalWorkflowServiceStubs.blockingStub()).thenReturn(temporalWorkflowServiceBlockingStub)
30+
metricClient = mockk(relaxed = true)
31+
temporalWorkflowServiceBlockingStub = mockk()
32+
temporalWorkflowServiceStubs = mockk()
33+
every { temporalWorkflowServiceStubs.blockingStub() } returns temporalWorkflowServiceBlockingStub
3334
serviceStubsWrapped =
3435
WorkflowServiceStubsWrapped(
3536
temporalWorkflowServiceStubs,
@@ -44,10 +45,7 @@ internal class WorkflowServiceStubsWrappedTest {
4445
fun testListClosedWorkflowExecutions() {
4546
val request = ListClosedWorkflowExecutionsRequest.newBuilder().build()
4647
val response = ListClosedWorkflowExecutionsResponse.newBuilder().build()
47-
Mockito
48-
.`when`(temporalWorkflowServiceBlockingStub.listClosedWorkflowExecutions(request))
49-
.thenAnswer { throw unavailable() }
50-
.thenReturn(response)
48+
every { temporalWorkflowServiceBlockingStub.listClosedWorkflowExecutions(request) } throws unavailable() andThen response
5149

5250
val actual = serviceStubsWrapped.blockingStubListClosedWorkflowExecutions(request)
5351
Assertions.assertEquals(response, actual)
@@ -57,10 +55,7 @@ internal class WorkflowServiceStubsWrappedTest {
5755
fun testListOpenWorkflowExecutions() {
5856
val request = ListOpenWorkflowExecutionsRequest.newBuilder().build()
5957
val response = ListOpenWorkflowExecutionsResponse.newBuilder().build()
60-
Mockito
61-
.`when`(temporalWorkflowServiceBlockingStub.listOpenWorkflowExecutions(request))
62-
.thenAnswer { throw unavailable() }
63-
.thenReturn(response)
58+
every { temporalWorkflowServiceBlockingStub.listOpenWorkflowExecutions(request) } throws unavailable() andThen response
6459

6560
val actual = serviceStubsWrapped.blockingStubListOpenWorkflowExecutions(request)
6661
Assertions.assertEquals(response, actual)

airbyte-commons-temporal-core/src/test/kotlin/io/airbyte/commons/temporal/queue/BasicQueueTest.kt

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ package io.airbyte.commons.temporal.queue
77
import com.fasterxml.jackson.databind.annotation.JsonDeserialize
88
import io.airbyte.commons.temporal.WorkflowClientWrapped
99
import io.airbyte.metrics.MetricClient
10+
import io.mockk.mockk
11+
import io.mockk.slot
12+
import io.mockk.spyk
13+
import io.mockk.verify
1014
import io.temporal.activity.ActivityOptions
1115
import io.temporal.client.WorkflowClient
1216
import io.temporal.client.WorkflowOptions
@@ -17,11 +21,6 @@ import org.junit.jupiter.api.AfterAll
1721
import org.junit.jupiter.api.Assertions.assertEquals
1822
import org.junit.jupiter.api.BeforeAll
1923
import org.junit.jupiter.api.Test
20-
import org.mockito.ArgumentCaptor
21-
import org.mockito.ArgumentMatchers.any
22-
import org.mockito.Mockito.mock
23-
import org.mockito.Mockito.spy
24-
import org.mockito.Mockito.verify
2524
import java.time.Duration
2625
import java.util.concurrent.CountDownLatch
2726

@@ -34,8 +33,10 @@ data class TestQueueInput(
3433
data class Builder(
3534
var input: String? = null,
3635
) {
36+
@Suppress("unused")
3737
fun input(input: String) = apply { this.input = input }
3838

39+
@Suppress("unused")
3940
fun build() = TestQueueInput(input = input!!)
4041
}
4142
}
@@ -90,7 +91,7 @@ class BasicQueueTest {
9091
client = testEnv.workflowClient
9192

9293
consumer = TestConsumer()
93-
activity = spy(QueueActivityImpl(consumer))
94+
activity = spyk(QueueActivityImpl(consumer))
9495
worker.registerActivitiesImplementations(activity)
9596
testEnv.start()
9697
}
@@ -104,8 +105,8 @@ class BasicQueueTest {
104105

105106
@Test
106107
fun testRoundTrip() {
107-
val workflowClient = spy(WorkflowClientWrapped(client, mock(MetricClient::class.java)))
108-
val optionsCaptor = ArgumentCaptor.forClass(WorkflowOptions::class.java)
108+
val workflowClient = spyk(WorkflowClientWrapped(client, mockk<MetricClient>(relaxed = true)))
109+
val optionsSlot = slot<WorkflowOptions>()
109110

110111
val producer = TemporalMessageProducer<TestQueueInput>(workflowClient)
111112
val message = TestQueueInput("boom!")
@@ -114,8 +115,8 @@ class BasicQueueTest {
114115

115116
// Since publishing is async, wait on the latch
116117
consumer.latch.await()
117-
verify(activity).consume(Message(message))
118-
verify(workflowClient).newWorkflowStub<QueueWorkflow<TestQueueInput>>(any(), optionsCaptor.capture())
119-
assertEquals(messageId, optionsCaptor.value.workflowId)
118+
verify { activity.consume(Message(message)) }
119+
verify { workflowClient.newWorkflowStub<QueueWorkflow<TestQueueInput>>(any(), capture(optionsSlot)) }
120+
assertEquals(messageId, optionsSlot.captured.workflowId)
120121
}
121122
}

airbyte-connector-rollout-worker/build.gradle.kts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ dependencies {
3232

3333
testImplementation(libs.temporal.testing)
3434
testImplementation(libs.mockk)
35-
testImplementation(libs.mockito.inline)
3635
testImplementation(libs.bundles.micronaut.test)
3736
}
3837

0 commit comments

Comments
 (0)