junit - Correct way to use Mockito for JdbcOperation -
i new mockito , trying cover following source code:
jdbcoperations.update(insertrolequery,new object[]{"menuname","submenuname","subsubmenuname","aa","bb","cc","role"}); in query taking 7 string parameters. have written mockito test case code , it's covering source code not sure whether it's correct way or not.
when(jdbcoperations.update(mockito.anystring(), new object[]{mockito.anystring(),mockito.anystring(),mockito.anystring(),mockito.anystring(),mockito.anystring(),mockito.anystring(),mockito.anystring()})).thenthrow(runtimeexception); please suggest if doing right way or not.
thanks
as per the docs, can either use exact values, or argument matchers, not both @ same time:
warning on argument matchers:
if using argument matchers, arguments have provided matchers.
if mix them, in sample, mockito complain similar to
org.mockito.exceptions.misusing.invaliduseofmatchersexception: invalid use of argument matchers! 2 matchers expected, 1 recorded: -> @ mytest.shouldmatcharray(mytest.java:38) exception may occur if matchers combined raw values: //incorrect: somemethod(anyobject(), "raw string"); when using matchers, arguments have provided matchers. example: //correct: somemethod(anyobject(), eq("string matcher")); more info see javadoc matchers class. in case don't seem care array contents, can use any():
when(jdbcoperation.update(anystring(), any())).thenthrow(runtimeexception); if want @ least check number of parameters, can use either
when(jdbcoperation.update(anystring(), argthat(array -> array.length == 7))).thenthrow(runtimeexception); when(jdbcoperation.update(anystring(), argthat(arraywithsize(7)))).thenthrow(runtimeexception); if you're interested in matching values, can use additionalmatchers.aryeq(expectedarray), or mockito.eq(expectedarray) has special implementation arrays, fell first 1 expresses intent in clearer way.
when(jdbcoperation.update(anystring(), aryeq(new object[]{"whatever"}))).thenthrow(runtimeexception);
Comments
Post a Comment