gpt4 book ai didi

java - 测试主类退出状态

转载 作者:行者123 更新时间:2023-12-02 10:29:44 25 4
gpt4 key购买 nike

我编写了简单的主类,它需要两个参数“app firstOne secondaryOne”。我想编写 Junit 测试来检查用户输入的参数数量是否正确

public static void main(String[] args) {
if (args.length == 2) {
someMethod();

System.exit(1);
} else
System.exit(0);
}

我想检查返回哪个退出状态。

我发现使用

@Rule
public final ExpectedSystemExit exit = ExpectedSystemExit.none();

但它找不到lib(我现在使用的是Junit版本4.13.beta-1)

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

您知道如何编写测试来检查传递给程序的参数数量是否正确吗?

最佳答案

您应该添加 System Rules图书馆。

Maven:

<dependency>
<groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId>
<version>1.19.0</version>
<scope>test</scope>
</dependency>

Gradle :

testCompile group: 'com.github.stefanbirkner', name: 'system-rules', version: '1.19.0'

关于java - 测试主类退出状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53682377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com