Skip to content

Commit c87c5c2

Browse files
authored
Reused the MockRequestResponsePairList typealias. Used single mock dispatcher instead of collection. (#411)
1 parent 78ec3ec commit c87c5c2

File tree

4 files changed

+14
-21
lines changed

4 files changed

+14
-21
lines changed

architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/rule/WebServerRule.kt

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,25 +8,25 @@ import org.junit.runner.Description
88
import org.junit.runners.model.Statement
99

1010
class WebServerRule(
11-
private val lazyMockDispatchers: Lazy<Collection<ResponseBinder>>,
11+
private val lazyMockDispatcher: Lazy<ResponseBinder>,
1212
private val lazyResponseStore: Lazy<ResponseStore>
1313
) : TestRule {
1414
override fun apply(base: Statement, description: Description): Statement =
1515
WebServerInitializationStatement(
16-
lazyMockDispatchers,
16+
lazyMockDispatcher,
1717
lazyResponseStore,
1818
base,
1919
description
2020
)
2121

2222
private class WebServerInitializationStatement(
23-
private val lazyMockDispatchers: Lazy<Collection<ResponseBinder>>,
23+
private val lazyMockDispatcher: Lazy<ResponseBinder>,
2424
private val lazyResponseStore: Lazy<ResponseStore>,
2525
private val base: Statement,
2626
private val description: Description
2727
) : Statement() {
2828
val responseStore by lazy { lazyResponseStore.value }
29-
val mockDispatchers by lazy { lazyMockDispatchers.value }
29+
val mockDispatcher by lazy { lazyMockDispatcher.value }
3030

3131
override fun evaluate() {
3232
val requestResponses = description.requestResponseIds()
@@ -37,29 +37,24 @@ class WebServerRule(
3737
)
3838
}
3939

40-
mockDispatchers.forEach { dispatcher ->
41-
requestResponses.forEach { requestResponse ->
42-
dispatcher
43-
.bindResponse(requestResponse.request, requestResponse.responseFactory)
44-
}
40+
requestResponses.forEach { requestResponse ->
41+
mockDispatcher.bindResponse(requestResponse)
4542
}
4643
val stubbedResponseKeys = requestResponses
4744
.map { requestResponse -> requestResponse.request.url }
4845
.toSet()
4946

5047
base.evaluate()
5148

52-
val usedResponseKeys = mockDispatchers
53-
.flatMap { dispatcher -> dispatcher.usedEndpoints }
54-
.toSet()
49+
val usedResponseKeys = mockDispatcher.usedEndpoints.toSet()
5550

5651
val unusedResponseKeys = stubbedResponseKeys - usedResponseKeys
5752
check(unusedResponseKeys.isEmpty()) {
5853
"${unusedResponseKeys.size} unused stubbed URLs:\n[" +
5954
unusedResponseKeys.joinToString("]\n[") + "]"
6055
}
6156

62-
mockDispatchers.forEach(ResponseBinder::reset)
57+
mockDispatcher.reset()
6358
}
6459

6560
private fun Description.requestResponseIds() =

architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/server/MockDispatcher.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class MockDispatcher :
2121

2222
override var onWebSocketMessage: (String) -> Unit = {}
2323

24-
override fun bindResponse(request: MockRequest, response: MockResponseFactory) {
25-
responses[request.url] = response
24+
override fun bindResponse(requestResponseFactory: MockRequestResponseFactory) {
25+
responses[requestResponseFactory.request.url] = requestResponseFactory.responseFactory
2626
}
2727

2828
override fun reset() {
Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
package com.mitteloupe.whoami.test.server
22

3-
import com.mitteloupe.whoami.test.server.response.MockResponseFactory
4-
53
interface ResponseBinder {
6-
var onWebSocketMessage: (String) -> Unit
4+
fun bindResponse(requestResponseFactory: MockRequestResponseFactory)
75

86
val usedEndpoints: Set<String>
97

10-
fun bindResponse(request: MockRequest, response: MockResponseFactory)
11-
128
fun reset()
9+
10+
var onWebSocketMessage: (String) -> Unit
1311
}

architecture/instrumentation-test/src/main/java/com/mitteloupe/whoami/test/test/BaseTest.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ abstract class BaseTest {
6060
lateinit var composeIdlingResources: @JvmSuppressWildcards Collection<ComposeIdlingResource>
6161

6262
private val webServerRule = WebServerRule(
63-
lazy { listOf(mockDispatcher) },
63+
lazy { mockDispatcher },
6464
lazy { responseStore }
6565
)
6666

0 commit comments

Comments
 (0)