Testing a private method in Java -



Testing a private method in Java -

i beginner in programming in java. facing problem in testing private method against unit testing tool. there no errors when compile , getting expected results when come in values in fields.

here part of code wrote:

public class title { private string getnewstring() { if (newname == null || "".equals(newname)) { homecoming newname; } else { homecoming (newname.substring(0,1).touppercase() + newname.substring(1).tolowercase()); } } }

the next testing tool giving me error: <method> not exist, or spelled wrong

@test public void testgetnewstring() { //string parameter class[] paramstring = new class[1]; paramstring[0] = string.class; seek { //load title @ runtime class cls = class.forname("title"); object obj = cls.newinstance(); //call printitstring method, pass string param method method = cls.getdeclaredmethod("getnewstring", paramstring); method.setaccessible(true); string returnval1 = (string) method.invoke(obj, new string("peter")); string returnval2 = (string) method.invoke(obj, new string("peter")); string returnval3 = (string) method.invoke(obj, new string("peter")); int modifiers = method.getmodifiers(); assertequals("getnewstring not format name correctly. \n", "peter", returnval1); assertequals("getnewstring not format name correctly. \n", "peter", returnval2); assertequals("getnewstring not format name correctly. \n", "peter", returnval3); assertequals("method \"getnewstring()\" not marked private. \n", true, modifier.isprivate(modifiers)); } grab (nosuchmethodexception e) { // field not exist throw new assertionerror("method \"getnewstring()\" not exist, or spelled wrong. \n"); } grab (exception e) { e.printstacktrace(); } }

any suggestions making mistake?

your method calling:

method method = cls.getdeclaredmethod("getnewstring", paramstring);

and define paramstring as:

class[] paramstring = new class[1];

this mean looks method signature uses 1 parameter. getnewstring doesn't have parameters.

removing paramstring should trick.

java

Comments

Popular posts from this blog

Delphi change the assembly code of a running process -

json - Hibernate and Jackson (java.lang.IllegalStateException: Cannot call sendError() after the response has been committed) -

C++ 11 "class" keyword -