Re: JUnit + System.exit(-1): Looking for alternatives



Lasse Reichstein Nielsen <lrn@xxxxxxxxxx> writes:

Try modifying the TestCase to run with a security manager that
prevents calling System.exit, then catch the SecurityException.

Something like (untested!):

And now that it has been tested, it turns out to fail in several ways.

This one seems to work, though:

---
public class NoExitTestCase extends TestCase {

protected static class ExitException extends SecurityException {
public final int status;
public ExitException(int status) {
super("There is no escape!");
this.status = status;
}
}

private static class NoExitSecurityManager extends SecurityManager {
@Override
public void checkPermission(Permission perm) {
// allow anything.
}
@Override
public void checkPermission(Permission perm, Object context) {
// allow anything.
}
@Override
public void checkExit(int status) {
super.checkExit(status);
throw new ExitException(status);
}
}

@Override
protected void setUp() throws Exception {
super.setUp();
System.setSecurityManager(new NoExitSecurityManager());
}

@Override
protected void tearDown() throws Exception {
System.setSecurityManager(null); // or save and restore original
super.tearDown();
}

public void testNoExit() throws Exception {
System.out.println("Printing works");
}

public void testExit() throws Exception {
try {
System.exit(42);
} catch (ExitException e) {
assertEquals("Exit status", 42, e.status);
}
}
}
---
I'm sure it can be done prettier, but it gives the general idea.

/L
--
Lasse Reichstein Nielsen - lrn@xxxxxxxxxx
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'
.



Relevant Pages

  • Re: JUnit + System.exit(-1): Looking for alternatives
    ... prevents calling System.exit, then catch the SecurityException. ... public class SecurityTestCase extends TestCase { ... public void tearDown() throws Exception { ...
    (comp.lang.java.programmer)
  • Re: Requesting tips, comments for an EDT thread-safe game architecture
    ... Use a Window or JWindow and do active rendering. ... synchronize some parts of your code but the more you can avoid that the ... public void myRenderingLoop() { ...
    (comp.lang.java.gui)
  • Re: Problem with overloaded functions
    ... Then use keyword override in the derived class Point3D, ... public void ReadXml2 ... > public class Point2D: IXmlSerializable ... > class1.cs: warning CS0108: The keyword new is required on> 'Point3D.WriteXml' because it hides inherited member ...
    (microsoft.public.dotnet.languages.csharp)
  • Requesting tips, comments for an EDT thread-safe game architecture
    ... me wonder whether my general game architecture was thread safe or not. ... public static void mainthrows InterruptedException, ... public void windowActivated{ ...
    (comp.lang.java.gui)
  • Re: JFrame window dimensions
    ... public void run{ ... Also, I've found it instructive to run the program in a profiler while resizing the window, paying particular attention to the event queue. ... This is by far the shortest of the 3 books I studied learning Java, and yet I find it the most helpful for quick answers. ...
    (comp.lang.java.programmer)