java - Testing model classes in Android -


i need test model classes of project. here 1 of classes

public class tmydata{     private mvar1;     private mvar2;     ...      private tmydata(){     }      public static tmydata fromstring(string str){         ...     }       public string tostring(){         ...     }   } 

simply, test should be:

1) create random tmydata

2) call tostring

3) call fromstring

4) check same

the problem create random tmydata. testing class separate 1 (tmydatatest), cannot following:

tmydata p = new tmydata();  //i can't, because private p.mvar1 = ...; // can't because private 

i don't want change variables/methods visibilities because of testing... , don't want user have access private variables (no getters nor setters)

many people must have had similar problems, projects have own model. proper way test it?

i can see solution doing reflection.

field f = tmydata.getclass().getdeclaredfield("mvar1"); //nosuchfieldexception f.setaccessible(true); f.set(tmydata,datatoset); 

Comments

Popular posts from this blog

android - getbluetoothservice() called with no bluetoothmanagercallback -

sql - ASP.NET SqlDataSource, like on SelectCommand -

ios - Undefined symbols for architecture armv7: "_OBJC_CLASS_$_SSZipArchive" -