Chenxi said:
i've written a small programme and want to test the main method. the
problem
is that if the parameters are wrong and the programme terminates, JUnit
cannot detect it and the test result would be pass. if i use
System.exit(1),
JUnit cannot catch the termination status and the result will be that
the
test is still running.
Two solutions.
Treat it like a GUI and make the main method really simple. Passing the
logic over to another method and just handling the exit itself.
public static void main(String[] args) {
int status = mainImpl(args);
if (status != 0) {
System.exit(status);
}
}
Use System.setSecurityManager (once). Before System.exit exits, it calls
SecurityManager.checkExit(int status). There you can assert the exit
code and then throw an expected SecurityException. Off the top of my head:
public class ExpectedSecurityException extends SecurityException {
public ExpectedSecurityException(String message) {
super(message);
}
}
public void TestingSecurityManager extends SecurityManager {
public static TestingSecurityManager install() {
SecurityManager existing = System.getSecurityManager();
if (existing instanceof TestingSecurityManager) {
return (TestingSecurityManager)existing;
} else if (existing == null) {
TestingSecurityManager security =
new TestingSecurityManager();
System.setSecurityManager(security);
return security;
} else {
ClassLoader loader = existing.getClass().getClassLoader();
Assert.fail(
"SecurityManager already set "+
"<"+existing+">, "+
"class: "+existing.getClass()+", "+
(loader==null? "bootstap class loader" : (
"class loader: <"+loader+"> "+
"class loader class: "+loader.getClass()
))
);
}
}
private Integer exitCode;
public void reset() {
exitCode = null;
}
public void expectExit(int exitCode) {
this.exitCode = exitCode;
}
@Override
public void checkPermission(Permission perm) { }
@Override
public void checkPermission(Permission perm, Object context) { }
@Override
public void checkExit(int status) {
if (exitCode != null) {
if (exitCode == status) {
throw new ExpectedSecurityException(
"Pass: Correct status code"
);
} else {
Assert.assertEqual(
"System.exit(int status) wrong code",
exitCode.intValue(),
status
);
}
}
}
}
private static void assertMainExit(
int expectedExitCode, String[] args
) {
TestingSecurityManager security =
TestingSecurityManager.install();
security.expectExit(exitCode);
try {
A.main(args);
fail("Expected exit, status code: "+exitCode);
} catch ExpectedSecurityException exc) {
// Expected.
} finally {
security.reset();
}
}
The technique is useful embedding third party programs (subject to
copyright).
Tom Hawtin