Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to use "any" with a lambda? #520

Open
austinarbor opened this issue Jun 21, 2024 · 0 comments
Open

How to use "any" with a lambda? #520

austinarbor opened this issue Jun 21, 2024 · 0 comments

Comments

@austinarbor
Copy link

I am trying to mock the AWS Kotlin SDK, but getting an error I cannot figure out. I am trying to stub out the SDK function
public suspend inline fun StsClient.getFederationToken(crossinline block: GetFederationTokenRequest.Builder.() -> Unit): GetFederationTokenResponse = getFederationToken(GetFederationTokenRequest.Builder().apply(block).build())

When using any<GetFederationTokenRequest.Builder.() -> Unit>(), I get the error

java.lang.NullPointerException: Cannot invoke "kotlin.jvm.functions.Function1.invoke(Object)" because "block$iv" is null

Can you let me know what I'm doing wrong, or if there is a better way of achieving this?

Thank you!

build.gradle.kts:

plugins {
    kotlin("jvm") version "2.0.0"
    application
}

dependencies {
    implementation("aws.sdk.kotlin:sts:1.2.38")
    testImplementation("org.mockito:mockito-junit-jupiter:5.12.0")
    testImplementation("org.junit.jupiter:junit-jupiter:5.10.2")
    testImplementation("org.mockito.kotlin:mockito-kotlin:5.3.1")
    testImplementation("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.8.1")
}

test

@Test
  fun testSts() = runTest {
    val client = mock<StsClient> {
      onBlocking { getFederationToken(any<GetFederationTokenRequest.Builder.() -> Unit>()) } doReturn GetFederationTokenResponse {
        credentials = Credentials {
          accessKeyId = "accessKey"
          secretAccessKey = "secretKey"
          sessionToken = "sessionToken"
          expiration = Instant.now().toSdkInstant()
        }
      }
    }

    client.getFederationToken {
      name = "test-name"
      policy = "{}"
    }

    argumentCaptor<GetFederationTokenRequest.Builder.() -> Unit>().apply {
      verify(client.getFederationToken(capture()))
      val builtRequest = GetFederationTokenRequest {
        firstValue.invoke(this)
      }
      assertEquals(builtRequest.name, "test-nam")
    }
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant