Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > java unit test

Reply
Thread Tools

java unit test

 
 
Chenxi
Guest
Posts: n/a
 
      07-29-2005
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);
}

......

}
=================
--

 
Reply With Quote
 
 
 
 
Ingo R. Homann
Guest
Posts: n/a
 
      07-29-2005
Hi Chenxi,

Chenxi wrote:
> 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

 
Reply With Quote
 
 
 
 
jan V
Guest
Posts: n/a
 
      07-29-2005
> How can i let the unit test know whether the programme fails or
> succeeds?


How about TestCase's fail(String msg) method? No good?


 
Reply With Quote
 
Chenxi
Guest
Posts: n/a
 
      07-29-2005
seems to be an alternative way

 
Reply With Quote
 
Chenxi
Guest
Posts: n/a
 
      07-29-2005
where to put fail(...)?
dont work to me

 
Reply With Quote
 
Thomas Hawtin
Guest
Posts: n/a
 
      07-29-2005
Chenxi wrote:
>
> 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
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
contradicting unit test regarding blocks pass, bug in unit/test? timr Ruby 2 11-20-2010 06:30 AM
Test::Unit - Ruby Unit Testing Framework Questions Bill Mosteller Ruby 0 10-22-2009 02:02 PM
All of us know about unit test. But what's a unit? Bill David Java 2 06-18-2008 12:40 AM
unit--, a unit test framework for C++ VvanN C++ 5 04-28-2006 10:01 AM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments