java unit test

C

Chenxi

hello

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.

How can i let the unit test know whether the programme fails or
succeeds?

==================
public class A{

public A{}

.......

public static void main(String[] args){
if (args.length<2){
System.out.println("wrong parameters");
return;
// System.exit(1);
}

......

}
=================
--
 
I

Ingo R. Homann

Hi Chenxi,
public static void main(String[] args){
if (args.length<2){
System.out.println("wrong parameters");
return;
// System.exit(1);
}

Do it in a different way:

class X {

public void main(String[] args) {
if(args.length!=2) {
printUsage();
System.exit(1);
}
go(args[0],args[1]);
}

public void go(String a, String b) {...}

}

Now, don't test the main-method but test the go-method instead!

Hth,
Ingo
 
T

Thomas Hawtin

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top