-1

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?

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

Ps, I can not change the function.

eiCh
  • 33
  • 1
  • 7
  • possible duplicate of [JUnit test for System.out.println()](http://stackoverflow.com/questions/1119385/junit-test-for-system-out-println) – Stefan Birkner May 11 '15 at 15:45

1 Answers1

0

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());
    }
}
  • First off, thank you! Secondly, When I try to use an object StandardOutputStreamLog as proposed, costing me an error "StandardOutputStreamLog cannot be resolved to a type" – eiCh May 18 '15 at 10:54