From 9e29b19db2ec403be1291445d2a84e4d4e6516d1 Mon Sep 17 00:00:00 2001 From: nowakweronika Date: Wed, 27 Mar 2024 16:49:50 +0100 Subject: [PATCH] Add starting and finished methods to Rule. --- .../MockWebServerRule.kt | 50 +++++++------------ .../MockWebServerRuleTest.kt | 45 +++++++++-------- 2 files changed, 43 insertions(+), 52 deletions(-) diff --git a/mockwebserver-extensions/src/main/java/com/appunite/mockwebserverextensions/MockWebServerRule.kt b/mockwebserver-extensions/src/main/java/com/appunite/mockwebserverextensions/MockWebServerRule.kt index c78df6f..83701e8 100644 --- a/mockwebserver-extensions/src/main/java/com/appunite/mockwebserverextensions/MockWebServerRule.kt +++ b/mockwebserver-extensions/src/main/java/com/appunite/mockwebserverextensions/MockWebServerRule.kt @@ -2,13 +2,13 @@ package com.appunite.mockwebserverextensions import com.appunite.mockwebserver_interceptor.TestInterceptor import com.appunite.mockwebserverextensions.intercept.UrlOverrideInterceptor -import com.appunite.mockwebserverextensions.util.MultipleFailuresError import com.appunite.mockwebserverextensions.util.ResponseGenerator import okhttp3.Interceptor +import okhttp3.internal.closeQuietly import okhttp3.mockwebserver.MockWebServer import org.junit.rules.TestWatcher import org.junit.runner.Description -import org.junit.runners.model.Statement +import org.junit.runners.model.MultipleFailureException import java.util.logging.Logger open class MockWebServerRule( @@ -20,39 +20,27 @@ open class MockWebServerRule( } open var mockDispatcher: MockDispatcher = MockDispatcher() + open var mockWebServer: MockWebServer? = null override fun register(response: ResponseGenerator) = mockDispatcher.register(response) override fun clear() = mockDispatcher.clear() - override fun apply(base: Statement, description: Description): Statement { - return object : Statement() { - override fun evaluate() { - MockWebServer().use { server -> - server.dispatcher = mockDispatcher - TestInterceptor.testInterceptor = - interceptor ?: UrlOverrideInterceptor(server.url("/")) - LOG.info("TestInterceptor installed") - try { - base.evaluate() - } catch (e: Throwable) { - if (mockDispatcher.errors.isEmpty()) { - throw e - } else { - throw MultipleFailuresError( - "An test exception occurred, but we also found some not mocked requests", - buildList { - add(e) - addAll(mockDispatcher.errors) - } - ) - } - } finally { - LOG.info("TestInterceptor uninstalled") - TestInterceptor.testInterceptor = null - } - } - } - } + override fun starting(description: Description?) { + val server = MockWebServer() + server.dispatcher = mockDispatcher + TestInterceptor.testInterceptor = interceptor ?: UrlOverrideInterceptor(server.url("/")) + LOG.info("TestInterceptor installed") + mockWebServer = server + } + + override fun finished(description: Description) { + LOG.info("TestInterceptor uninstalled") + TestInterceptor.testInterceptor = null + + mockWebServer?.closeQuietly() + mockWebServer = null + + MultipleFailureException.assertEmpty(mockDispatcher.errors) } } diff --git a/mockwebserver-extensions/src/test/kotlin/com/appunite/mockwebserverextensions/MockWebServerRuleTest.kt b/mockwebserver-extensions/src/test/kotlin/com/appunite/mockwebserverextensions/MockWebServerRuleTest.kt index 7e430af..5be3ef3 100644 --- a/mockwebserver-extensions/src/test/kotlin/com/appunite/mockwebserverextensions/MockWebServerRuleTest.kt +++ b/mockwebserver-extensions/src/test/kotlin/com/appunite/mockwebserverextensions/MockWebServerRuleTest.kt @@ -28,6 +28,7 @@ import org.hamcrest.TypeSafeMatcher import org.junit.Rule import org.junit.Test import org.junit.rules.ExpectedException +import org.junit.runners.model.MultipleFailureException import strikt.api.expectThat import strikt.assertions.contains import strikt.assertions.first @@ -80,6 +81,13 @@ class MockWebServerRuleTest { @Test fun testWhenMockIsCleared_requestGetsDefault404() { + expectedException.expect( + matcher { + expectThat(it).isA().message.isNotNull() + .contains("Request: GET https://example.com/user, there are no mocks") + } + ) + mockWebServerRule.register { _ -> MockResponse().setBody("Hello, World!") } @@ -92,7 +100,12 @@ class MockWebServerRuleTest { @Test fun testWhenNoRequestIsMocked_throw404() { - val response = executeRequest("https://example.com/not_mocked") + mockWebServerRule.register { + expectThat(it).url.path.isEqualTo("/mocked404") + + MockResponse().setResponseCode(404) + } + val response = executeRequest("https://example.com/mocked404") expectThat(response).code.isEqualTo(404) } @@ -101,9 +114,11 @@ class MockWebServerRuleTest { fun testTestFailsAndThereIsSomeMockingIssue_theRootOfTheFailureIsOnTheFirstPlace() { expectedException.expect( matcher { - expectThat(it).isA().message.isNotNull() - .contains("An test exception occurred, but we also found some not mocked requests") - expectThat(it).isA().get(MultipleFailuresError::failures) + expectThat(it).isA().message.isNotNull() + .contains("There were 2 errors") + expectThat(it) + .isA() + .get("failures") { failures } .first().isA() } ) @@ -121,8 +136,9 @@ class MockWebServerRuleTest { .contains("Request: GET https://example.com/not_mocked, there are no mocks") expectThat(it) - .isA() - .get(MultipleFailuresError::failures) + + .isA() + .get("failures") { failures } .last() .isA() .and { @@ -154,8 +170,8 @@ class MockWebServerRuleTest { } expectThat(it) - .isA() - .get(MultipleFailuresError::failures) + .isA() + .get("failures") { failures } .last() .isA() .and { @@ -176,19 +192,6 @@ class MockWebServerRuleTest { throw CustomException } - @Test - fun testWhenRequestUrlIsDifferentThenMocked_throw404() { - mockWebServerRule.register { - expectThat(it).url.path.isEqualTo("/different_url") - - MockResponse().setBody("Hello, World!") - } - - val response = executeRequest("https://example.com/not_mocked") - - expectThat(response).code.isEqualTo(404) - } - @Test fun testWhenRequestIsMade_passedUrlAndMethodAreCorrect() { val slot = executeRequestAndGetMockedArgumentRequest()