Hi! :raised_hand_with_fingers_splayed::skin-tone-2...
# getting-started
s
Hi! 🖐🏻 please help me slove the problem i have a method with two parameters (java):
Copy code
public PushSmsResponse sendPushOrSms(PushSmsRequest pushSmsRequest, PushSmsSendRequest pushSmsSendRequest)
and now i try to fix test (Kotlin!) :
Copy code
whenever(
    pushSmsClient.sendPushOrSms(
        PushSmsRequest().apply {

            … some parameters, setters etc.
        },
        any<PushSmsSendRequest>() - problem HERE!
how can I write Kotlin code for analog from java :
Copy code
(ArgumentMatchers.any() as PushSmsSendRequest)
in java it work’s. Mock method with any object PushSmsSendRequest type, how can i do this in Kotlin?
error message from thread:
Invalid 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 deprecated
k
Just as the error message says, since the second parameter that you pass to
sendPushOrSms
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
.
s
Omg i think 2 matchers it’s a count, not quantity
Copy code
pushSmsClient.sendPushOrSms(
    pushSmsRequest,
    eq(any<PushSmsSendRequest>())
)
it’s started, but mock doesn’t work correctly
when i call …client.sendPushOrSMS() than i get null (
k
No, not
eq(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.)
s
just a minute, i check
sorry but i’am don’t understand
Copy code
whenever(
    pushSmsClient.sendPushOrSms(
        pushSmsRequest,
        eq(pushSmsSendRequest)
    )
).thenReturn(PushSmsResponse())
i try this
k
The error message says:
This exception may occur if matchers are combined with raw values.
And indeed you are combining a matcher (
any
) with a raw value (
pushSmsRequest
).
Copy code
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:
Copy code
whenever(
    pushSmsClient.sendPushOrSms(
        eq(pushSmsRequest),         <--- MATCHER
        any<PushSmsSendRequest>()   <--- MATCHER     - All are matchers, so ok.
    )
s
just in case
Copy code
pushSmsRequest
it’s a object
calling method :
Copy code
public PushSmsResponse sendPushOrSms(PushSmsRequest pushSmsRequest, PushSmsSendRequest pushSmsSendRequest) {
Copy code
whenever(
    pushSmsClient.sendPushOrSms(
        eq(pushSmsRequest),         <--- MATCHER
        any<PushSmsSendRequest>()   <--- MATCHER     - All are matchers, so ok.
    )
doesn’t work(((