Java:如何测试调用System.exit()的方法? [英] Java: How to test methods that call System.exit()?

查看:154
本文介绍了Java:如何测试调用System.exit()的方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些方法应该在某些输入上调用 System.exit()。不幸的是,测试这些情况会导致JUnit终止!将方法调用放在新线程中似乎没有帮助,因为 System.exit()终止JVM,而不仅仅是当前线程。是否有任何常见的处理方式?例如,我可以为 System.exit()替换存根吗?

I've got a few methods that should call System.exit() on certain inputs. Unfortunately, testing these cases causes JUnit to terminate! Putting the method calls in a new Thread doesn't seem to help, since System.exit() terminates the JVM, not just the current thread. Are there any common patterns for dealing with this? For example, can I subsitute a stub for System.exit()?

有问题的类实际上是一个命令行工具,我试图在JUnit中测试。也许JUnit根本不适合这份工作?建议使用补充回归测试工具(最好是与JUnit和EclEmma很好地集成的东西)。

The class in question is actually a command-line tool which I'm attempting to test inside JUnit. Maybe JUnit is simply not the right tool for the job? Suggestions for complementary regression testing tools are welcome (preferably something that integrates well with JUnit and EclEmma).

推荐答案

确实, Derkeiler.com 建议:


  • 为什么 System.exit()

  • Why System.exit() ?

为什么不抛出未经检查的异常,而不是使用System.exit(whateverValue)终止?在正常使用中,它会一直漂移到JVM的最后一个捕获器并关闭你的脚本(除非你决定在途中捕获它,这可能在某一天有用)。

Instead of terminating with System.exit(whateverValue), why not throw an unchecked exception? In normal use it will drift all the way out to the JVM's last-ditch catcher and shut your script down (unless you decide to catch it somewhere along the way, which might be useful someday).

在JUnit场景中,它将被JUnit框架捕获,它将报告
这样的测试失败并顺利移动到下一个。

In the JUnit scenario it will be caught by the JUnit framework, which will report that such-and-such test failed and move smoothly along to the next.




  • 防止 System.exit()实际退出JVM:

    • Prevent System.exit() to actually exit the JVM:

    • 尝试修改TestCase以使用防止调用System的安全管理器运行.exit,然后捕获SecurityException。

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



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






      2012年12月更新:


      Update December 2012:

      将提议使用在评论中 href =http://stefanbirkner.github.com/system-rules/ =noreferrer> 系统规则 ,用于测试代码的JUnit(4.9+)规则的集合它使用 java.lang.System

      最初由 Stefan Birkner 于2011年12月他的回答

      Will proposes in the comments using System Rules, a collection of JUnit(4.9+) rules for testing code which uses java.lang.System.
      This was initially mentioned by Stefan Birkner in his answer in December 2011.

      System.exit(…)
      




      使用 ExpectedSystemEx它 规则验证是否已调用 System.exit(...)

      您可以验证退出状态也是如此。

      Use the ExpectedSystemExit rule to verify that System.exit(…) is called.
      You could verify the exit status, too.

      例如:

      public void MyTest {
          @Rule
          public final ExpectedSystemExit exit = ExpectedSystemExit.none();
      
          @Test
          public void noSystemExit() {
              //passes
          }
      
          @Test
          public void systemExitWithArbitraryStatusCode() {
              exit.expectSystemExit();
              System.exit(0);
          }
      
          @Test
          public void systemExitWithSelectedStatusCode0() {
              exit.expectSystemExitWithStatus(0);
              System.exit(0);
          }
      }
      

      这篇关于Java:如何测试调用System.exit()的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆