在Eclipse中将命令行参数传递给JUnit [英] Passing command line arguments to JUnit in Eclipse

查看:79
本文介绍了在Eclipse中将命令行参数传递给JUnit的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

全部

我目前正在使用JUnit 4编写测试用例.我对JUnit相当陌生,发现很难测试带有参数的主类.我已经通过以下方式为我的JUnit测试类指定了参数:

I am currently using JUnit 4 for writing test cases. I am fairly new to JUnit and finding it difficult to test my main class which takes arguments. I have specified the arguments to my JUnit test class by:

1>右键单击JUnit测试类
2>转到运行方式->运行配置
3>选择参数"选项卡并指定一个值(我输入了无效的参数,即主类希望将命令行参数转换为int,并且我传递了无法转换为int的字符串值)

1 > Right click JUnit test class
2 > Goto Run As -> Run Configurations
3 > Select the Arguments tab and specify a value (I have entered an invalid argument i.e. the main class expects the command line argument to be converted to an int and I am passing a String value that cannot be converted to int)

但是,如果无法将命令行参数转换为int,则我正在测试的main类会抛出IllegalArgumentException.但是,JUnit不会将testMain()方法显示为ErrorFailure.我认为我的设置不适合JUnit类.谁能指导我哪里出问题了

However, the main class that I am testing, if the command line argument cannot be converted to a int, than I throw IllegalArgumentException. However, the JUnit does not show the testMain() method as Error or Failure. I don't think my setup is right for the JUnit class. Can anyone please guide me where I am going wrong

推荐答案

要测试您的类main方法,只需编写如下内容:

To test your class main method simply write something like:

@Test(expected = IllegalArgumentException.class)
public void testMainWithBadCommandLine()
{
     YourClass.main(new String[] { "NaN" });
}

这篇关于在Eclipse中将命令行参数传递给JUnit的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆