Using Mockk to mock out a singleton object to ignore Auth journey

I am using Mockk and I have the need to intercept when an API client is being created.

The API client does a bunch of REST stuff that I don't want to happen inside of its constructor. I have tried a bunch of things but can't seem to find a way to not actually run the constructor and just return something.

I don't want to actually run anything when the object is created. Is this possible?

I've tried:

Class I want to mock:

class TestApi(config) {
   auth = Auth.authenticate(config) // Don't want this specifically to run
}

Caller:

fun createClient() {
    return TestApi(ConfigObj())
}

Then in the test

@Test
fun `sample code`() {
  mockkConstructor(TestApi::class)
  every { anyConstructed<TestApi>() } returns FakeInstance()
  
  // other test stuff always fails as TestApi() still runs the full init with the auth flow
}


Comments

Popular posts from this blog

Today Walkin 14th-Sept

Hibernate Search - Elasticsearch with JSON manipulation

Spring Elasticsearch Operations