在使用TestNG做单元测试时,需要测试的代码中出现System.exit(0),导致单元测试还未结束程序就停止了。解决方法如下:

public class TestMain {

    public static void main(String args[]) {
NoExitSecurityManager manager = new NoExitSecurityManager();
System.setSecurityManager(manager);
new TestNGApp("test/TestUtils.xml").run();
manager.exitFilter = false;
System.exit(0);
} /**
* 拦截系统退出
*/
private static class NoExitSecurityManager extends SecurityManager { boolean exitFilter = true; @Override
public void checkPermission(Permission perm) {
} @Override
public void checkPermission(Permission perm, Object context) {
} @Override
public void checkExit(int status) {
super.checkExit(status);
if (exitFilter) {
throw new ExitException(status);
}
}
} protected static class ExitException extends SecurityException { private static final long serialVersionUID = 1L;
public final int status; public ExitException(int status) {
super("成功拦截System.ext(0)!");
this.status = status;
}
} }
  

参考:https://stackoverflow.com/questions/309396/java-how-to-test-methods-that-call-system-exit

  http://coding.derkeiler.com/Archive/Java/comp.lang.java.programmer/2008-04/msg02603.html

java拦截处理System.exit(0)的更多相关文章

  1. JAVA System.exit(0) 和 System.exit(1) 的区别

    System.exit(int state) 方法都是来结束当前运行的java虚拟机.所有System.exit(1).System.exit(0) 执行后都会退出程序. state为0时时正常退出, ...

  2. Java 中的System.exit

    在java 中退出程序,经常会使用System.exit(1) 或 System.exit(0). 查看System.exit()方法的源码,如下 /** * Terminates the curre ...

  3. system.exit(0) vs system.exit(1)

    2.解析 查看java.lang.System的源代码,我们可以找到System.exit(status)这个方法的说明,代码如下: /** * Terminates the currently ru ...

  4. [Java123] Java中的System.exit

    参考:http://www.cnblogs.com/xwdreamer/archive/2011/01/07/2297045.html System.exit(int  status) 方法 java ...

  5. System.exit(0);和finish();,push原理

    今天师姐问我安卓后台的问题,想起几年前做进制转换的时候特意研究了一下怎么才能「不驻留内存地退出」.虽然Android不推荐用户手动关闭进程,但是在那个内存捉襟见肘的年代,不得不考虑内存. 首先直接按b ...

  6. System.exit(0)和System.exit(1)区别(转)

    转:http://www.cnblogs.com/xwdreamer/archive/2011/01/07/2297045.html 1.参考文献 http://hi.baidu.com/accpzh ...

  7. android Activity类中的finish()、onDestory()和System.exit(0) 三者的区别

    android Activity类中的finish().onDestory()和System.exit(0) 三者的区别 Activity.finish() Call this when your a ...

  8. System.exit(0)和System.exit(1)区别

    System.exit(0)是将你的整个虚拟机里的内容都停掉了 ,而dispose()只是关闭这个窗口,但是并没有停止整个application exit() .无论如何,内存都释放了!也就是说连JV ...

  9. System.exit(0)作用

    System.exit(0)作用   public class HelloGoodbye{ try{ System.out.println(“Hello World”); System.exit(0) ...

随机推荐

  1. Python appium搭建app自动化测试环境

    appium做app自动化测试,环境搭建是比较麻烦的. 也是很多初学者在学习app自动化之时,花很多时间都难跨越的坎. 但没有成功的环境,就没有办法继续后续的使用. 在app自动化测试当中,我们主要是 ...

  2. vue keep-alive内置缓存组件

    1.当组件在keep-alive被切换时将会执行activeted和deactiveted两个生命周期 2.inlude 正则表达式或字符串 ,只有符合条件的组件会被缓存 exclude正则表达式或字 ...

  3. Mysql5.7数据导出提示--secure-file-priv选项问题的解决方法

    mysql可使用into outfile参数把表中的数据到处到csv,示例如下: select user_id from weibo_comment into outfile '/home/dazha ...

  4. ESLint学习小记

    一.关于配置文件,优先级从上到下: eslintrc.js .eslintrc.yaml .eslintrc.yml .eslintrc.json .eslintrc package.json 在官方 ...

  5. spring security 学习笔记

    官方文档

  6. Unsafe 的简单使用

    Unsafe 简介 Unsafe 是sun.misc包中的一个类,可以通过内存偏移量操作类变量/成员变量 Unsafe 用途 AQS(AbstractQueuedSynchronizer) 常用作实现 ...

  7. Java 多线程 - 生产者消费者问题

    https://www.cnblogs.com/hckblogs/p/7858545.html

  8. Large-Margin Softmax Loss for Convolutional Neural Networks

    paper url: https://arxiv.org/pdf/1612.02295 year:2017 Introduction 交叉熵损失与softmax一起使用可以说是CNN中最常用的监督组件 ...

  9. JSONP以及端口

    跨域的方式有多种今天我呢,给大家带来的是JSONP接口的操作和接口 JSONP的接口到处都有 今天先拿BOOS直聘的来给大家演示一遍吧 首先找到boss官网:https://www.zhipin.co ...

  10. 微信小程序开发学习(二)

    一些官方API 总结了一些官方API,便于之后有用时针对性查找(发现官方给了好多好用的API)官方API文档 基础 wx.canIUse:判断小程序的API,回调,参数,组件等是否在当前版本可用,返回 ...