Shumilin Alexandr
10/31/2022, 11:03 AMpublic PushSmsResponse sendPushOrSms(PushSmsRequest pushSmsRequest, PushSmsSendRequest pushSmsSendRequest)
and now i try to fix test (Kotlin!) :
whenever(
pushSmsClient.sendPushOrSms(
PushSmsRequest().apply {
… some parameters, setters etc.
},
any<PushSmsSendRequest>() - problem HERE!
how can I write Kotlin code for analog from java :
(ArgumentMatchers.any() as PushSmsSendRequest)
in java it work’s. Mock method with any object PushSmsSendRequest type, how can i do this in Kotlin?Shumilin Alexandr
10/31/2022, 11:06 AMInvalid use of argument matchers!
2 matchers expected, 1 recorded:
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
but anyObject() marked as deprecatedKlitos Kyriacou
10/31/2022, 12:19 PMsendPushOrSms
is a matcher, the first parameter also needs to be a matcher, not an actual object. Therefore, you need to use it with a matcher such as eq
.Shumilin Alexandr
10/31/2022, 12:30 PMShumilin Alexandr
10/31/2022, 12:49 PMpushSmsClient.sendPushOrSms(
pushSmsRequest,
eq(any<PushSmsSendRequest>())
)
it’s started, but mock doesn’t work correctlyShumilin Alexandr
10/31/2022, 12:50 PMKlitos Kyriacou
10/31/2022, 12:54 PMeq(any<PushSmsSendRequest>())
, but the other parameter. any
is already a matcher, you don't need to create a matcher from a matcher. You need to apply eq
to the other parameter because that one is not a matcher. So eq(pushSmsRequest)
. (And make sure pushSmsRequest
has a sensible equals
function.)Shumilin Alexandr
10/31/2022, 12:56 PMShumilin Alexandr
10/31/2022, 12:59 PMShumilin Alexandr
10/31/2022, 12:59 PMwhenever(
pushSmsClient.sendPushOrSms(
pushSmsRequest,
eq(pushSmsSendRequest)
)
).thenReturn(PushSmsResponse())
i try thisKlitos Kyriacou
10/31/2022, 1:04 PMThis exception may occur if matchers are combined with raw values.And indeed you are combining a matcher (
any
) with a raw value (pushSmsRequest
).
whenever(
pushSmsClient.sendPushOrSms(
pushSmsRequest, <--- RAW VALUE
any<PushSmsSendRequest>() <--- MATCHER - You should not mix matchers with raw values.
)
Also, the error message tells you how to fix this:
When using matchers, all arguments have to be provided by matchers.So do this:
whenever(
pushSmsClient.sendPushOrSms(
eq(pushSmsRequest), <--- MATCHER
any<PushSmsSendRequest>() <--- MATCHER - All are matchers, so ok.
)
Shumilin Alexandr
10/31/2022, 1:08 PMpushSmsRequest
it’s a objectShumilin Alexandr
10/31/2022, 1:08 PMpublic PushSmsResponse sendPushOrSms(PushSmsRequest pushSmsRequest, PushSmsSendRequest pushSmsSendRequest) {
Shumilin Alexandr
10/31/2022, 1:08 PMwhenever(
pushSmsClient.sendPushOrSms(
eq(pushSmsRequest), <--- MATCHER
any<PushSmsSendRequest>() <--- MATCHER - All are matchers, so ok.
)
doesn’t work(((