Java 通过junit测试main方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36349827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Testing main method by junit
提问by user2719152
I wrote a class which takes input from console and arguments in main method. The main method calls different methods for different console inputs and it calls different function for different arguments. So i want to test this main method with Junit by mimicking these inputs from a file. how can i do it? Is there any special provision in junit to test the main method of the class?
我写了一个类,它接受来自控制台的输入和 main 方法中的参数。main 方法为不同的控制台输入调用不同的方法,它为不同的参数调用不同的函数。所以我想通过模仿文件中的这些输入来使用 Junit 测试这个主要方法。我该怎么做?junit 有没有什么特殊的规定来测试类的main 方法?
采纳答案by G_H
To provide the input from a file, make a FileInputStream
and set that as the System.in
stream. You'll probably want to set the original back after the main method has finished to make sure anything using it later still works (other tests, JUnit itself...)
要从文件提供输入,请创建一个FileInputStream
并将其设置为System.in
流。您可能希望在 main 方法完成后重新设置原始方法,以确保以后使用它的任何内容仍然有效(其他测试,JUnit 本身...)
Here's an example:
下面是一个例子:
@Test
public void testMain() throws IOException {
System.out.println("main");
String[] args = null;
final InputStream original = System.in;
final FileInputStream fips = new FileInputStream(new File("[path_to_file]"));
System.setIn(fips);
Main.main(args);
System.setIn(original);
}
In your actual code you'll want to handle any IOExceptions and use something better than a full path to the file (get it via the classloader), but this gives you the general idea.
在您的实际代码中,您需要处理任何 IOExceptions 并使用比文件完整路径更好的方法(通过类加载器获取它),但这为您提供了总体思路。
回答by David Mumladze
You can call main method from junit test like this:
您可以像这样从 junit 测试调用 main 方法:
YourClass.main(new String[] {"arg1", "arg2", "arg3"});
But since main method is void and does not return anything, you should test object that changed after main invocation;
但是由于 main 方法是 void 并且不返回任何内容,因此您应该测试在 main 调用后更改的对象;
Here is Link How do I test a method that doesn't return anything?
这是链接如何测试不返回任何内容的方法?
回答by Michael Lloyd Lee mlk
IMO the best way to test the main method is by having the main method do absolutely nothing but set up the world and kick it off. This way one simple integration test gives the answer "has the world been set up".
IMO 测试主要方法的最佳方法是让主要方法除了设置世界并启动它之外什么都不做。通过这种方式,一个简单的集成测试给出了“世界已经建立”的答案。
Then all the other questions become much easier to answer.
然后所有其他问题变得更容易回答。
The main method calls different methods for different console inputs and it calls different function for different arguments.
main 方法为不同的控制台输入调用不同的方法,它为不同的参数调用不同的函数。
It should not, it should call someService.somemethod(args)
. This service gets tested like any other.
它不应该,它应该调用someService.somemethod(args)
. 该服务像其他任何服务一样经过测试。
So i want to test this main method with Junit by mimicking these inputs from a file. how can i do it?
所以我想通过模仿文件中的这些输入来使用 Junit 测试这个主要方法。我该怎么做?
Either some form of fakes injected in or the use of the TemporaryFolder JUnit rule.
注入某种形式的假货或使用 TemporaryFolder JUnit 规则。