java - Getting Line number of code executed -
i writing application automated generation of unit test cases.the application takes input .java file , process's find methods. methods invoked using test data using following command. method.invoke(classobj,methodarg); wish find lines executed int invoked method calculate coverage. have tried doing using stacktrace() gives line number of application code. can me this. , utterly lost. highly appreciated. current code is:
runnable myrunnable; myrunnable = new runnable(){ public void run(){ try { int returnvalint = (int) fm.invoke(fobj, fargs); system.out.println(new exception().getstacktrace()[0].getlinenumber()); } catch (illegalaccessexception ex) { logger.getlogger(helloworldframe.class.getname()).log(level.severe, null, ex); } catch (illegalargumentexception ex) { logger.getlogger(helloworldframe.class.getname()).log(level.severe, null, ex); } catch (invocationtargetexception ex) { logger.getlogger(helloworldframe.class.getname()).log(level.severe, null, ex); } } }; thread th= new thread(myrunnable, "th"); th.start();
you can read how coverage tools collect stats here.
but before that, you're trying isn't going use - whole point of test test intent of piece of code. need know boundaries are, expect results be.
for example:
public class math { public static long add(long a, long b) { return + b; } public static long multiply(long a, long b) { return * b; } } public class mathtest { @test public void testadd() { asserttrue(5, math.add(2, 3)); } @test public void testmultiply() { asserttrue(6, math.multiply(2, 3)); } }
you unable generate test code method signatures alone. have introduce test data, feasibly define method applies to, in more simple cases such data become test case , you've achieved introduction of additional layer of complexity.
at end of day unit test test data test specification can defined well.
Comments
Post a Comment