Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
71 views
in Technique[技术] by (71.8m points)

java - how to verify a parameter in unit test

I have a run function which calls repository.startFlling(orderStorage.flOrder!!.id, action) and I am trying to verify repository.startFlling is called with correct parameters.

I could mock orderStorage.flOrder!!.id but action gets created locally in the function and is based on DateTime,

I tried creating an action in my testcase but that does not match with the one dynamically created in the actual function.

how can I deal with this scenario/

Here is my run function with startTime which gets created when called.

 val startTime = DateTime()
lateinit var orderEquipment: Equipment

override suspend fun run(params: Params): Either<Failure, GenericResponse> {
    this.orderEquipment = params.equipment
    val action = TimestampedAction(
        app.session.user.id, null, startTime
    )
    val result = repository.startFlling(orderStorage.flOrder!!.id, action)
    result.fold(::handleStartFllingFailure, ::handleStartFllingSuccess)
    return result
}

Test case

@Test
fun `when StartFllingUseCase is invoked then call startFlling in flOrderRespository with correct parameters`() {
    val id = "1"
    val userId = "userId"
    val flOrderId = "flOrderId"
    val action: TimestampedAction = TimestampedAction(userId, null, DateTime())
    val equipment: Equipment = mock()

    runBlocking {
        whenever(user.id).thenReturn(userId)
        whenever(orderStorage.flOrder).thenReturn(flOrder)
        whenever(flOrder.id).thenReturn(flOrderId)
        whenever(equipment.times).thenReturn(times)
        whenever(flOrderRepository.startFlling(any(), any()))
            .thenReturn(Either.Right(GenericResponse(true)))

        startFllingUseCase.run(StartFllingUseCase.Params(equipment))

        verify(flOrderRepository).startFlling(flOrderId, action)
    }
}

error

org.mockito.exceptions.base.MockitoAssertionError: There were multiple verification failures:
1. Argument(s) are different! Wanted:
flOrderRepository.startFlling(
    "flOrderId",
    com.xx.xxx.objects.florder.equipment.TimestampedAction@4567e53d
);
-> at com.xx.xxx.clean.florder.data.repository.FlOrderRepository.startFlling(FlOrderRepository.kt:17)
Actual invocations have different arguments:
flOrderRepository.startFlling(
    "flOrderId",
    com.xx.xxx.objects.florder.equipment.TimestampedAction@66ec9390
);

Can you please suggest how can I fix this

thanks R

question from:https://stackoverflow.com/questions/65928879/how-to-verify-a-parameter-in-unit-test

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You can use argumentCaptor like described here.

Or if you use mockitokotlin2 there is a function named ArgThat which creates new argument matcher based on predicate.

/**
 * Creates a custom argument matcher.
 * `null` values will never evaluate to `true`.
 *
 * @param predicate An extension function on [T] that returns `true` when a [T] matches the predicate.
 */
inline fun <reified T : Any> argThat(noinline predicate: T.() -> Boolean): T {
    return Mockito.argThat { arg: T? -> arg?.predicate() ?: false } ?: createInstance(
          T::class
    )
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...