Skip to content

Instantly share code, notes, and snippets.

@billydh
Last active January 12, 2020 04:10
Show Gist options
  • Select an option

  • Save billydh/2237a86460438b817be993cb007c4150 to your computer and use it in GitHub Desktop.

Select an option

Save billydh/2237a86460438b817be993cb007c4150 to your computer and use it in GitHub Desktop.
RouteTest.kt with Open Weather API response loaded and WebTestClient auto configuration annotation
package io.codebrews.wiremockdemo
import com.github.tomakehurst.wiremock.client.WireMock.*
import com.github.tomakehurst.wiremock.junit.WireMockRule
import org.junit.ClassRule
import org.junit.Test
import org.junit.runner.RunWith
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient
import org.springframework.boot.test.context.SpringBootTest
import org.springframework.http.HttpHeaders
import org.springframework.http.HttpStatus
import org.springframework.http.MediaType
import org.springframework.test.context.junit4.SpringRunner
import org.springframework.test.web.reactive.server.WebTestClient
@SpringBootTest
@RunWith(SpringRunner::class)
@AutoConfigureWebTestClient
class RouteTest {
private val logger: Logger = LoggerFactory.getLogger(javaClass)
@Autowired
private lateinit var client: WebTestClient
companion object {
@ClassRule
@JvmField
val wireMockRule = WireMockRule(8089)
}
private fun stubResponse(url: String, responseBody: String, responseStatus: Int = HttpStatus.OK.value()) {
stubFor(get(url)
.willReturn(aResponse()
.withStatus(responseStatus)
.withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.withBody(responseBody))
)
}
private val apiResponseFileName = "openweather-api-response.json"
private val openWeatherApiResponse: String? = this::class.java.classLoader.getResource(apiResponseFileName)?.readText()
@Test
fun `test open weather api response is loaded`(){
logger.info(openWeatherApiResponse)
assert(openWeatherApiResponse != null)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment