Junit——命令行参数
我刚刚开始学习 JUnit。我的代码如下:
public class MyClass {
private void verify(args) {...}
private void process(clientoptions) {...}
public static void main(String[] args) {
verify(args);
//get client and do something
.....
// some more code here....
........
// and then
process(clientoptions);
}
}
如何在 Junit 中编写测试,然后发送不同的命令行参数。
谢谢
I have just started learning JUnit. I have code as follows:
public class MyClass {
private void verify(args) {...}
private void process(clientoptions) {...}
public static void main(String[] args) {
verify(args);
//get client and do something
.....
// some more code here....
........
// and then
process(clientoptions);
}
}
How do you write a test in Junit and then send in different command line arguments.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
简单的!
但是,为了便于测试,请考虑将
MyClass
重构为MyClassMain
和MyClass
,其中前者仅解析和验证命令行参数并使用 <代码>客户端选项。这样您就不会违反单一责任原则,并且您可以使用
main
之外的业务逻辑,例如在 servlet 中或移动设备上。而且...测试更简单。Simple!
However for ease of testing consider refactoring
MyClass
intoMyClassMain
andMyClass
where the former only parses and verifies command line arguments and calls the latter class withclientoptions
.This way you don't violate Single responsibility principle and you can use the business logic outside of
main
, for instance in servlet or on a mobile. Also... testing is simpler.