【问题标题】:How to test print that made by function?如何测试由功能制作的打印?
【发布时间】:2015-05-11 15:37:33
【问题描述】:

我有一个应该打印字符串的函数。 我想通过 JUnit 测试一下,真的是打印它应该打印的功能。 怎么做?

例如,这是我的函数:

public static void test(int a){
    System.out.println(a + "^2=" + (a*a));
}

我想检查 a = 3 是否正在打印 3^2=9

Ps,我无法更改功能。

【问题讨论】:

标签: junit console output


【解决方案1】:

你为什么不试试这个。希望它对你有用。

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());
    }
}

【讨论】:

  • 首先,谢谢!其次,当我尝试按照建议使用对象 StandardOutputStreamLog 时,会导致错误“StandardOutputStreamLog 无法解析为类型”
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-28
  • 1970-01-01
  • 2021-10-16
  • 2013-03-07
  • 1970-01-01
  • 2021-10-30
  • 1970-01-01
相关资源
最近更新 更多