5

I need to provide some mock object array of this type "TypeA[]".

I am trying to do this but getting classcastexception:

List mockList = Mockito.anyListOf(TypeA.class);

when(someService.create(Mockito.any(TypeB.class), (TypeA[])mockList.toArray())).thenReturn(1);

1 Answer 1

7

The error message told you clearly :

You cannot use argument matchers outside of verification or stubbing.
Examples of correct usage of argument matchers:
    when(mock.get(anyInt())).thenReturn(null);
    doThrow(new RuntimeException()).when(mock).someVoidMethod(anyObject());
    verify(mock).someMethod(contains("foo"))

The method call to object returned by Mockito.anyListOf can only inside stubbing or verification .

you can simply do that to mock array :

when(mockTest.create(any(TypeB.class), any(TypeA[].class))).thenReturn(1);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.