Hello all!
So far this question has the Java people at work stumped. It might be because
this is simply impossible, but no one can say for certain.
In an attempt to put some JUnit tests against some existing code, I wanted to
test the command line processing of the main() method. Having found the
technique at http://c2.com/cgi/wiki?JavaTestingTactics to capture the
System.out.print() and System.err.print() output, I had some "working" tests
that verify that bad and/or missing arguments are properly noted.
However, the normal case is a problem. In my test, I'd like to define a class
B (see below) and call B.main("-w", "x", "-y", "z") which calls run("x", "z")
and then I can assertEquals("x", b.someArg).
The problem is that A.main() calls "(new A()).run(...)". I know that
B.main(args) will actually call A.main(args), but is there something that can
replace "new A()" so that B.main() results in a call to B.run()? The analog is
using "this.getClass()" instead of "A", but since A.main() is static, there is
no "this".
Is there a technique to accomplish this? (no pun intended)
-Rob
P.S. I know that I can equivalently refactor the "blah, blah, blah" and "arg
checking" into separate functions and then test /those/, but the original idea
has me curious.
A.java
======
public class A {
public static void main(String[] args) {
// blah, blah, blah,
// arg checking
if (noErrors) {
(new A()).run(moreArgs, otherArgs);
}
}
public void run(String someArg, String someOtherArg) {
// yada, yada, yada
}
}
ATest.java
======
public B extends A {
public void run(String someArg, String someOtherArg) {
this.someArg = someArg;
this.someOtherArg = someOtherArg;
}
String someArg;
String someOtherArg;
}
|