javaexit(0)_java拦截处理System.exit(0)

在TestNG单元测试中遇到System.exit(0)导致测试提前终止的问题,解决方案是使用NoExitSecurityManager拦截系统退出。通过创建自定义安全管理器并覆盖checkExit方法,可以在不结束程序的情况下模拟退出行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在使用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;

}

}

}

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

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

Java 中的System.exit

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

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

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

[Java123] Java中的System.exit

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

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

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

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

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

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

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

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

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

System.exit(0)作用

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

随机推荐

python 语法常用 lambda

Python中lambda表达式学习 http://blog.youkuaiyun.com/imzoer/article/details/8667176

5-17 Hashing (25分)

The task of this problem is simple: insert a sequence of distinct positive integers into a hash tabl ...

【转】invokeRequired属性和 invoke()方法

C#中禁止跨线程直接访问控件,InvokeRequired是为了解决这个问题而产生的,当一个控件的InvokeRequired属性值为真时,说明有一个创建它以外的线程想访问它. 此时它将会在内部调用n ...

Design Pattern Memo 备忘录设计模式

本设计模式就是简单地记录当前状态.然后利用记录的数据恢复. 比方首先我们有一个类.类须要记录当前状态进行相关的工作的: class Memo; class Human { public: string ...

mysql字符串替换

数据库是Mysql的.我想把lesson表中的slide_path_dx字段中的类似 http://www.site.com/y/k/aote-02.rar 替换成E:\web\manhua\y\k\ ...

介绍一款轻量级js控件:easy.js

easy.js 组件高速入门 在使用 easy.js 的组件之前,假设能花上几分钟看看以下的一些简单的入门指南,在使用组件的时候你会更加的得心应手. 简单性 easy.js 的组件在 UI(界面) 层 ...

Python-图像处理库PIL图像变换transpose和transforms函数

1.transpose有这么几种模式FLIP_LEFT_RIGHT ,FLIP_TOP_BOTTOM ,ROTATE_90 ,ROTATE_180 ,ROTATE_270,TRANSPOSE ,TRA ...

深入理解PHP的运行模式

PHP运行模式有4钟:1)cgi 通用网关接口(Common Gateway Interface))2) fast-cgi 常驻 (long-live) 型的 CGI3) cli  命令行运行   ( ...

如何删除jsPlumb连接

I am playing with jsplumb, but I am not able to delete the connection between two divs having only t ...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值