Skip to content

Commit

Permalink
Add starting and finished methods to Rule.
Browse files Browse the repository at this point in the history
  • Loading branch information
nowakweronika committed Mar 27, 2024
1 parent 89c6e44 commit 9e29b19
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 52 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -80,6 +81,13 @@ class MockWebServerRuleTest {

@Test
fun testWhenMockIsCleared_requestGetsDefault404() {
expectedException.expect(
matcher<Any> {
expectThat(it).isA<Throwable>().message.isNotNull()
.contains("Request: GET https://example.com/user, there are no mocks")
}
)

mockWebServerRule.register { _ ->
MockResponse().setBody("Hello, World!")
}
Expand All @@ -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)
}
Expand All @@ -101,9 +114,11 @@ class MockWebServerRuleTest {
fun testTestFailsAndThereIsSomeMockingIssue_theRootOfTheFailureIsOnTheFirstPlace() {
expectedException.expect(
matcher<Any> {
expectThat(it).isA<MultipleFailuresError>().message.isNotNull()
.contains("An test exception occurred, but we also found some not mocked requests")
expectThat(it).isA<MultipleFailuresError>().get(MultipleFailuresError::failures)
expectThat(it).isA<MultipleFailureException>().message.isNotNull()
.contains("There were 2 errors")
expectThat(it)
.isA<MultipleFailureException>()
.get("failures") { failures }
.first().isA<CustomException>()
}
)
Expand All @@ -121,8 +136,9 @@ class MockWebServerRuleTest {
.contains("Request: GET https://example.com/not_mocked, there are no mocks")

expectThat(it)
.isA<MultipleFailuresError>()
.get(MultipleFailuresError::failures)

.isA<MultipleFailureException>()
.get("failures") { failures }
.last()
.isA<MultipleFailuresError>()
.and {
Expand Down Expand Up @@ -154,8 +170,8 @@ class MockWebServerRuleTest {
}

expectThat(it)
.isA<MultipleFailuresError>()
.get(MultipleFailuresError::failures)
.isA<MultipleFailureException>()
.get("failures") { failures }
.last()
.isA<MultipleFailuresError>()
.and {
Expand All @@ -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()
Expand Down

0 comments on commit 9e29b19

Please sign in to comment.