How to test print method in Java using Junit

I wrote a method that outputs the output to the console. How to check it?

public class PrinterForConsole implements Printer<Item>{

   public void printResult(List<Item> items) {
        for (Item item: items){
            System.out.println("Name: " + item.getName());
            System.out.println("Number: " + item.getNumber());

            }
        }
}

currently my test is as follows

public class TestPrinter{
    @Test
    public void printResultTest() throws Exception {
            (am figuring out what to put here)

        }
}

I read the solution on this post (thanks @Codebender and @KDM for highlighting this), but don't quite understand it. How does this solution test the print (list) method? Therefore, we ask him here again.

+4
source share
5 answers

Since you put it, you do not understand what the repeating question says, I will try to explain a little.

, System.setOut(OutputStream), , ( System.out.printX()), outputStream, .

, -

public void printTest() throws Exception {
      ByteArrayOutputStream outContent = new ByteArrayOutputStream();
      System.setOut(new PrintStream(outContent));

      // After this all System.out.println() statements will come to outContent stream.

     // So, you can normally call,
     print(items); // I will assume items is already initialized properly.

     //Now you have to validate the output. Let say items had 1 element.
     // With name as FirstElement and number as 1.
     String expectedOutput  = "Name: FirstElement\nNumber: 1" // Notice the \n for new line.

     // Do the actual assertion.
     assertEquals(expectedOutput, outContent.toString());
}
+10

- , PrintStream , PrintStream, ByteArrayOutputStream, , .

System.setOut, . , .

:

@Test
public void printTest() throws Exception {
    // Create our test list of items
    ArrayList<Item> items = new ArrayList<Item>();
    items.add(new Item("KDM", 1810));
    items.add(new Item("Roy", 2010));

    // Keep current System.out with us
    PrintStream oldOut = System.out;

    // Create a ByteArrayOutputStream so that we can get the output
    // from the call to print
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // Change System.out to point out to our stream
    System.setOut(new PrintStream(baos));

    print(items);

    // Reset the System.out
    System.setOut(oldOut);

    // Our baos has the content from the print statement
    String output = new String(baos.toByteArray());

    // Add some assertions out output
    assertTrue(output.contains("Name: KDM"));
    assertTrue(output.contains("Name: Roy"));

    System.out.println(output);
}

, print , System.out reset. setup teardown reset this.

+3

: -

@Test
public void out() {
System.out.print("hello");
assertEquals("helloworld", outContent.toString());
}
@Test
public void err() {
System.err.print("helloworld 1 ");
assertEquals("helloworld 1", errContent.toString());
}

: JUnit System.out.println()

0

- .

@Test
    public void printTest() throws Exception {
        OutputStream os = new ByteArrayOutputStream();
        System.setOut(os);
        objectInTest.print(items);
        String actualOutput = os.toString("UTF-8");
        assertEquals(expectedOutput, actualOutput);
    }
0

, ( ).

import org.junit.Test;
@Test
public void shouldPrintToConsole() throws Exception {
        Item testItem = new Item("Name", "Number");
        List<Item> items = Arrays.asList(testItem);

        Printer print = new Printer();
        printer.printOutput(items);
    }

(shouldPrintToConsole()) . , , , , .

0

All Articles