unit testing - Android JUnitTests - Is it possible to create a public static method for Method and Field Reflections? -
i know how can access private
method
or field
class
within test class
:
to access private method myclass
, named void dosomething()
:
import java.lang.reflect.invocationtargetexception; import java.lang.reflect.method; ... try { method method = myclass.class.getdeclaredmethod("dosomething", (class[])null); // (class[])null parameterless methods method.setaccessible(true); method.invoke(localinstanceofmyclass); } catch (nosuchmethodexception ex){ ex.printstacktrace(); } catch (illegalaccessexception ex){ ex.printstacktrace(); } catch (illegalargumentexception ex){ ex.printstacktrace(); } catch (invocationtargetexception ex) { ex.printstacktrace(); }
to access private field myclass
, named boolean myfield
:
import java.lang.reflect.field; ... try { field field = myclass.class.getdeclaredfield("myfield"); field.setaccessible(true); field.set(localinstanceofmyclass, true); // true value want assign myfield } catch (nosuchfieldexception ex){ ex.printstacktrace(); } catch (illegalaccessexception ex){ ex.printstacktrace(); } catch (illegalargumentexception ex){ ex.printstacktrace(); }
(source: https://stackoverflow.com/a/34658/1682559)
as can see quite lot of code putting private boolean
on true
or false
.
so, question: possible somehow make 2 public static methods
, 2 method
, 1 field
, can use in test classes
? clarify:
testmethodsclass.setprivatefield(... parameters ...); testmethodsclass.runprivatevoidmethod(... parameters ...); testmethodsclass.runprivatereturnmethod(... parameters ...);
example of parameters based on examples void dosomething()
en boolean myfield
above:
testmethodsclass.setprivatefield(localinstanceofmyclass, "myfield", true); testmethodsclass.runprivatevoidmethod(localinstanceofmyclass, "dosomething", null); // ps: null converted in method (class[])null`
thanks in advance responses.
ok, pretty easy..
testmethodsclass:
import java.lang.reflect.field; import java.lang.reflect.invocationtargetexception; import java.lang.reflect.method; public class testmethodsclass { public static void runprivatevoidmethod(object ob, string methodname, class<?>[] parameters){ try { method method = null; if(parameters == null){ class<?>[] nullparameter = (class[])null; method = ob.getclass().getdeclaredmethod(methodname, nullparameter); } else method = ob.getclass().getdeclaredmethod(methodname, parameters); if(method != null){ method.setaccessible(true); method.invoke(ob); } } catch (nosuchmethodexception ex){ ex.printstacktrace(); } catch (illegalaccessexception ex){ ex.printstacktrace(); } catch (illegalargumentexception ex){ ex.printstacktrace(); } catch (invocationtargetexception ex) { ex.printstacktrace(); } } public static object runprivatereturnmethod(object ob, string methodname, class<?>[] parameters){ object returnobject = null; try { if(parameters == null){ class<?>[] nullparameter = (class[])null; method method = ob.getclass().getdeclaredmethod(methodname, nullparameter); method.setaccessible(true); returnobject = method.invoke(ob); } else{ method method = ob.getclass().getdeclaredmethod(methodname, parameters); method.setaccessible(true); method.invoke(ob); } } catch (nosuchmethodexception ex){ ex.printstacktrace(); } catch (illegalaccessexception ex){ ex.printstacktrace(); } catch (illegalargumentexception ex){ ex.printstacktrace(); } catch (invocationtargetexception ex) { ex.printstacktrace(); } return returnobject; } public static void setprivatefield(object ob, string fieldname, object value){ try { field field = ob.getclass().getdeclaredfield(fieldname); field.setaccessible(true); field.set(ob, value); } catch (nosuchfieldexception ex){ ex.printstacktrace(); } catch (illegalaccessexception ex){ ex.printstacktrace(); } catch (illegalargumentexception ex){ ex.printstacktrace(); } } }
how call it:
// set private field "boolean myfield" myclass true testmethodsclass.setprivatefield(localinstanceofmyclass, "myfield", true); // run private method "dosomething()" myclass testmethodsclass.runprivatevoidmethod(localinstanceofmyclass, "dosomething", null); // run private method "dosomething()" myclass , assign return value local field boolean test = (boolean)testmethodsclass.runprivatereturnmethod(localinstanceofmyclass, "dosomething", null);
i haven't tested yet, don't know if works types. strings , booleans work pretty far can tell. more testing later on, when have spare time project.
Comments
Post a Comment