Skip to content

Commit

Permalink
Change method name bodyNotNull to requiredBody
Browse files Browse the repository at this point in the history
  • Loading branch information
Donghh0221 committed Apr 26, 2024
1 parent a25f776 commit de9a9ac
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
Expand Up @@ -43,10 +43,10 @@ inline fun <reified T : Any> RestClient.ResponseSpec.body(): T? =
body(object : ParameterizedTypeReference<T>() {})

/**
* Extension for [RestClient.ResponseSpec.body] providing a `bodyNotNull<Foo>()` variant
* Extension for [RestClient.ResponseSpec.body] providing a `requiredBody<Foo>()` variant
* To leverage Kotlin null safety, this extension throws a [NoSuchElementException] if the response body is null.
*/
inline fun <reified T : Any> RestClient.ResponseSpec.bodyNotNull(): T =
inline fun <reified T : Any> RestClient.ResponseSpec.requiredBody(): T =
body(object : ParameterizedTypeReference<T>() {}) ?: throw NoSuchElementException("Response body is null when a non-null type was expected.")

/**
Expand Down
Expand Up @@ -48,15 +48,15 @@ class RestClientExtensionsTests {
}

@Test
fun `ResponseSpec#bodyNotNull with reified type parameters`() {
responseSpec.bodyNotNull<List<Foo>>()
fun `ResponseSpec#requiredBody with reified type parameters`() {
responseSpec.requiredBody<List<Foo>>()
verify { responseSpec.body(object : ParameterizedTypeReference<List<Foo>>() {}) }
}

@Test
fun `ResponseSpec#bodyNotNull with null response throws NoSuchElementException`() {
fun `ResponseSpec#requiredBody with null response throws NoSuchElementException`() {
every { responseSpec.body(any<ParameterizedTypeReference<Foo>>()) } returns null
assertThrows<NoSuchElementException> { responseSpec.bodyNotNull<Foo>() }
assertThrows<NoSuchElementException> { responseSpec.requiredBody<Foo>() }
}

@Test
Expand Down

0 comments on commit de9a9ac

Please sign in to comment.