java unit test

Discussion in 'Java' started by Chenxi, Jul 29, 2005.

  1. Chenxi

    Chenxi Guest

    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);
    }

    ......

    }
    =================
    --
     
    Chenxi, Jul 29, 2005
    #1
    1. Advertising

  2. 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
     
    Ingo R. Homann, Jul 29, 2005
    #2
    1. Advertising

  3. Chenxi

    jan V Guest

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


    How about TestCase's fail(String msg) method? No good?
     
    jan V, Jul 29, 2005
    #3
  4. Chenxi

    Chenxi Guest

    seems to be an alternative way
     
    Chenxi, Jul 29, 2005
    #4
  5. Chenxi

    Chenxi Guest

    where to put fail(...)?
    dont work to me
     
    Chenxi, Jul 29, 2005
    #5
  6. 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/
     
    Thomas Hawtin, Jul 29, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Edvard Majakari
    Replies:
    4
    Views:
    701
    Edvard Majakari
    Feb 25, 2005
  2. VvanN
    Replies:
    5
    Views:
    509
    Phlip
    Apr 28, 2006
  3. Bill David
    Replies:
    2
    Views:
    280
    Arne Vajhøj
    Jun 18, 2008
  4. Bill Mosteller
    Replies:
    0
    Views:
    239
    Bill Mosteller
    Oct 22, 2009
  5. timr
    Replies:
    2
    Views:
    177
Loading...

Share This Page