views:

44

answers:

2

Using Junit coverage, it can't cover public static void main method. How can i cover it?

anyone can help me? urgent. Thanks

+4  A: 

After making sure it's testable just call it as any other public static method:

YourClass.main(new String[] {"parameter 1", "parameter 2"});

And assert the following state afterwards.

Boris Pavlović
I had wrote codes like yours, but coverage reports are displayed as you test didn't cover the main method.
Mike.Huang
Could you step through your test? If it does invoke the main method then the problem lies in the test coverage tool which excludes static methods.
Boris Pavlović
Unless you declared the main with variable arguments, shouldn't it be `YourClass.main(new String[] { "param 1", "param 2" });` ?
aioobe
yes,"main(String[] args)". and invoke it in testcase like yours
Mike.Huang
+1  A: 

Because it does not return anything, you cannot check the output. But what does it do? If it calls another method the write a test for that method. If it doesn't call other methods, then write a test for that.

fastcodejava
the objective of this issue is cover all lines of main method. i can't modify the content of main method. i had call the main method in junit test, but why the coverage can't cover it.
Mike.Huang