I have a function which should print string. I want to test through JUnit Is really the function printing what it is supposed to print. How to do it?
我有一个应该打印字符串的功能。我想通过JUnit进行测试真的是打印它应该打印的功能。怎么做?
for example, this my function:
例如,这是我的功能:
public static void test(int a){
System.out.println(a + "^2=" + (a*a));
}
and I want to check that for a = 3 is printing 3^2=9
我想检查a = 3是否打印3 ^ 2 = 9
Ps, I can not change the function.
Ps,我无法改变功能。
1 个解决方案
#1
Why don't you try this.Hope it will work for you.
你为什么不试试呢。希望它对你有用。
public void TestPrint {
@Rule
public final StandardOutputStreamLog log = new StandardOutputStreamLog();
@Test
public static void test(int a) {
System.out.print(a + "^2=" + (a*a));
assertEquals("3^2=9", log.getLog());
}
}
#1
Why don't you try this.Hope it will work for you.
你为什么不试试呢。希望它对你有用。
public void TestPrint {
@Rule
public final StandardOutputStreamLog log = new StandardOutputStreamLog();
@Test
public static void test(int a) {
System.out.print(a + "^2=" + (a*a));
assertEquals("3^2=9", log.getLog());
}
}