[英]Testing a simple hello world method
I have worked with junit test integration tests and controller tests in spring and usually we test the output of a method but when i tried to test a simple hello world in main method i had no idea how to go about it so will like to get any idea on what do write 我在春天使用过junit测试集成测试和控制器测试,通常我们测试一个方法的输出但是当我尝试在main方法中测试一个简单的hello world我不知道如何去做它所以想得到任何什么写的想法
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
}
}
This is the simple java class any idea how i can test it I tried to write something like this 这是简单的java类,任何想法我怎么测试它我试着写这样的东西
public void mainMethodTest() throws Exception{
System.out.println("hello world");
String[] args = null;
Assert.assertEquals(System.out.println("hello world"),App.main(args));
}
You could assign to the System.out
variable a ByteArrayOutputStream
object which you store the reference in a variable. 您可以为
System.out
变量分配一个ByteArrayOutputStream
对象,该对象将引用存储在变量中。
Then invoke your main()
method and assert that the String
content of the ByteArrayOutputStream
object contains the expected String
: 然后调用
main()
方法并断言ByteArrayOutputStream
对象的String
内容包含期望的String
:
@Test
public void main() throws Exception{
PrintStream originalOut = System.out; // to have a way to undo the binding with your `ByteArrayOutputStream`
ByteArrayOutputStream bos = new ByteArrayOutputStream();
System.setOut(new PrintStream(bos));
// action
App.main(null);
// assertion
Assert.assertEquals("hello world", bos.toString());
// undo the binding in System
System.setOut(originalOut);
}
Why does it work ? 它为什么有效?
bos.toString()
returns the "Hello World!"
bos.toString()
返回"Hello World!"
String
passed in the method under test: 在被测方法中传递的
String
:
System.out.println( "Hello World!" );
as after setting System.out
in this way : System.setOut(new PrintStream(bos));
在以这种方式设置
System.out
之后: System.setOut(new PrintStream(bos));
, the out
variable refers to a PrintStream
object that decorates the ByteArrayOutputStream
object referenced by the bos
variable. ,
out
变量引用一个PrintStream
对象,该对象修饰由bos
变量引用的ByteArrayOutputStream
对象。 So any System.out
invocations will write byte
s in the ByteArrayOutputStream
object. 因此,任何
System.out
调用都将在ByteArrayOutputStream
对象中写入byte
。
You can change your class this way 你可以这样改变你的课程
import java.io.PrintStream;
public class TestHelloWorld {
public final static void main(String[] args) {
doPrint(System.out);
}
static void doPrint(PrintStream ps) {
ps.println("Hello World");
}
}
and test the doPrint
function by providing your own PrintStream
you create around a ByteArrayOutputStream
: 并通过提供您在
ByteArrayOutputStream
周围创建的自己的PrintStream
测试doPrint
函数:
public void mainMethodTest() throws Exception{
ByteArrayOutputStream data = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(data, true, "UTF-8");
TestHelloWorld.doPrint(ps);
ps.flush();
Assert.assertEquals("Hello World") + System.getProperty("line.separator"), new String(data, "UTF-8"));
}
Another solution is to replace the system's PrintStream
by your own: 另一个解决方案是用你自己的系统替换系统的
PrintStream
:
System.setOut(new PrintStream(data, true, "UTF-8"));
but that's quite ugly and I try to avoid that. 但那很难看,我试着避免这种情况。 Above solution is more clear, easier to maintenance and you can be sure that no other part of a larger application is printing something to STDOUT while you do your test, leading to a failure of it.
以上解决方案更清晰,更易于维护,您可以确保在进行测试时,较大应用程序的其他任何部分都不会向STDOUT打印某些内容,从而导致其失败。
You can run Junit from a main method if thats what you mean. 如果这就是你的意思,你可以从主方法运行Junit。
public static void main( String[] args )
{
JUnitCore junit = new JUnitCore();
Result result = null;
try {
result = junit.run(MyTestClass.class);
}
catch(Exception e)
{
e.printStackTrace();
}
int passed = result.getRunCount()-result.getFailureCount();
}
public class MyTestClass{
@Test
public void testAllBrowsers(){
//test code and asserts
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.