android - Mockito verifying method calls -


i'm trying learn how use mockito , i'm hung on how go verifying method on object called x amount of times.

i have following test code

verify(record, times(1)).setvalue(mockito.any(string.class),mockito.any(string.class)); 

and following piece of production code i'm trying test

the string[]'s i'm setting iterate through

protected string[] columnnames = {"_id", "created_at", "updated_at", "name"}; protected columntype[] columntypes = {columntype.integer, columntype.timestamp, columntype.timestamp, columntype.text}; 

and production code that's in loop, iterating through string[]

for (int = 0; < columncount; i++) {              if (columnnames[i].equals("_id")) {                 record.setid(cursor.getint(0));             } else {                  switch (columntypes[i]) {                     case bool:                         record.setvalue(columnnames[i], cursor.getint(i));                         break;                     case text:                         record.setvalue(columnnames[i], cursor.getstring(i));                         break;                     case integer:                         record.setvalue(columnnames[i], cursor.getint(i));                         break;                     case timestamp:                         record.setvalue(columnnames[i], cursor.getlong(i));                         break;                     case long:                         record.setvalue(columnnames[i], cursor.getlong(i));                         break;                     case double:                         record.setvalue(columnnames[i], cursor.getdouble(i));                         break;                     default:                         record.setvalue(columnnames[i], "");                         break;                 }             }         } 

and error get

testdatasourcecanfindrecord(com.test.app.datasourcetest) time elapsed: 0.081 sec <<< failure! org.mockito.exceptions.verification.toomanyactualinvocations: customer.setvalue(, ); wanted 1 time: -> @ com.test.app.datasourcetest.testdatasourcecanfindrecord(datasourcetest.java:141) 3 times. undesired invocation: -> @ com.test.core.datasource.cursortorecord(datasource.java:210)

i'm expecting record.setvalue(string key, string value) called once because of "name" field in string[]. what's happening mockito registering record.setvalue(string key, long value) same thing record.setvalue(string key, string value), incorrect. line 210 setvalue in timestamp case. how can correct this?

if @ source code invocationmatcher, looks logic check method equality little more generous might think, , ignore method overloading in circumstances.

i'm not 100% sure, try replacing any(string.class) (which accepts any object of any type) isa(string.class), filter out invocations parameter not string. (anystring checks type in mockito 2.0 , beyond.) counterintuitively, any(foo.class) not mean "anything long it's foo", means "anything". this due change in mockito 2.0.

of course, tighten verification checks key or value equal expected value, i'm not sure how possible in case.


Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -