Unit test also methods in Kotlin

40 views Asked by At

I have a test function for the duplicate method of my service, and this method ends with a .also call, that runs after the return of the method. But my test isn't covering this.

I have the following code with a .also call at the end

    suspend fun duplicate(id: ObjectId): Foo {
        val foo = findById(id)
        reset(foo)
        return create(foo).also {
           externalClient.doSomething(foo)
        }
    }

And this is the test for this function

    @Test
    fun `should duplicate`() = runTest {
        wheneverBlocking { fooRepository.findById(any()) } doReturn Foo().apply {
            ...
        }
        wheneverBlocking { fooRepository.save(any()) } doAnswer { it.getArgument(0) }

        val res = fooService.duplicate(ObjectId())

        verify(externalclient, times(1)).doSomething(any())
    }

When I run the test, the verify doesn't pass, what can I do so it unit test the .also method?

0

There are 0 answers