java 监听junit结束_java – 在终止的代码上使用JUnit

您可以使用

System Rules:“JUnit规则集合,用于测试使用java.lang.System的代码.”

在他们的规则中,你有ExpectedSystemExit,下面是一个如何使用它的例子.我相信这是一个非常干净的解决方案.

import org.junit.Rule;

import org.junit.Test;

import org.junit.contrib.java.lang.system.Assertion;

import org.junit.contrib.java.lang.system.ExpectedSystemExit;

public class SystemExitTest {

@Rule

public final ExpectedSystemExit exit = ExpectedSystemExit.none();

@Test

public void noSystemExit() {

//passes

}

@Test

public void executeSomeCodeAFTERsystemExit() {

System.out.println("This is executed before everything.");

exit.expectSystemExit();

exit.checkAssertionAfterwards(new Assertion() {

@Override

public void checkAssertion() throws Exception {

System.out.println("This is executed AFTER System.exit()"+

" and,if exists,the @org.junit.After annotated method!");

}

});

System.out.println("This is executed right before System.exit().");

System.exit(0);

System.out.println("This is NEVER executed.");

}

@Test

public void systemExitWithArbitraryStatusCode() {

exit.expectSystemExit();

System.exit(0);

}

@Test

public void systemExitWithSelectedStatusCode0() {

exit.expectSystemExitWithStatus(0);

System.exit(0);

}

@Test

public void failSystemExit() {

exit.expectSystemExit();

//System.exit(0);

}

}

如果您使用maven,可以将其添加到您的pom.xml:

junit

junit

4.11

com.github.stefanbirkner

system-rules

1.3.0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值