zoukankan      html  css  js  c++  java
  • 当测试碰到system.exit()

    我们可能会碰到需要被测试的代码里面包含了system.exit()语句,我们只想退出当前测试用例,并不想退出整个测试(后面还有很多测试没跑).

    下面是解决办法,直接贴代码

       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 {

            public void checkPermission(Permission perm) {

                // allow anything.

            }

     

            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 IOException {

                      //通过该语句指定程序退出时的安全检查程序,我们可在里面动手脚

            System.setSecurityManager(new NoExitSecurityManager());

            overwriteLogFile("");

        }

     

        @Override

        protected void tearDown() throws IOException {

            System.setSecurityManager(null);

        }

     

            

        /**

         * Accuracy test for <code>main(String[])</code>. Tests situation when the

         * usage message should be given.

         *

         * @throws IOException

         *             to JUnit

         */

        public void test_main_Accuracy() throws IOException {

            String[] args = { "-help" };

            try {

                GLUIJobsDaemon.main(args);

      //程序退出时会执行NoExitSecurityManager里的方法(System.setSecurityManager(new 

      //  NoExitSecurityManager());

      //我们在checkExit方法时抛出异常ExitException 而不真正退出,随后异常被捕捉,见下面

            } catch (ExitException e) {

                assertEquals("Exit status should be right", 0, e.status);

            }

        }

     

  • 相关阅读:
    SQL多表查询:内连接、外连接(左连接、右连接)、全连接、交叉连接
    PL/SQL Developer基本用法
    Oracle中存储过程与函数的区别
    C#反射动态调用dll中的方法
    WebClient模拟发送Post请求
    C#字符串、字节数组和内存流间的相互转换
    C#调用WebService
    VS2008安装SP1补丁后智能提示从中文变为英文的解决办法
    C#流总结(文件流、内存流、网络流、BufferedStream、StreamReader/StreamWriter、TextReader/TextWriter)
    VS2008创建单元测试
  • 原文地址:https://www.cnblogs.com/ITEagle/p/1774878.html
Copyright © 2011-2022 走看看