Home > Mobile >  What kind of syntax is expected for a .class parameter in Mockito
What kind of syntax is expected for a .class parameter in Mockito

Time:07-19

I have a method that is defined like this Method(String, String, Something.class) Something can be x or y I want to test Method("","",x.class) but not Method("","",y.class)(which is also thrown in the same method) so I cannot use Mockito.when(ArgumentMatchers.anyString(),ArgumentMatchers.anyString(),ArgumentMatchers.any()).thenReturn(/../);

I need to know what to type in place of ArgumentMatchers.any() or within it so that Mockito doesn't attempt to convert x to y neither of which are interchangeable;

CodePudding user response:

Mockito.when(anyString(), anyString(), eq(x.class)).thenReturn(...) does the job.

I would however advise against using too broad matchers such as anyString und would recommend something along the lines of Mockito.when(eq("first"), eq("second"), eq(x.class)).thenReturn(...) This way also the expected parameter values are tested.

  • Related