> public void add(Item item) { > if (!item.header.equals(null)) { > this.items[this.index] = item; > this.index++; > } else { > System.out.println("Please header enter."); > } } 

I want to add a test for

 } else { System.out.println("Please header enter."); 

using construction

assertThat (???, is (???));

But what to substitute here if I have a console application? And the void method?

    1 answer 1

    Do not tie the application to the logging in the console. Use a separate logger object that will be passed the string and logging level. This is a standard, as is done in many large projects.

    Applications work on servers, there is no one to stare at the monitor and read the logs, so the messages are written where it is convenient for developers and administrators — to a file, to a socket, sent via http, whatever else. They may be in stdout, as in your case, but there must be a choice. And if in stdout, then System.out.println is in the logger code, and not in the place where logging occurs.

    And when you need to test the logger, you simply slip a test object instead, in which you will check that such and such a method has been called with such a string.

    There are already a lot of loggers written, I used log4j once, but I can't compare it to others.

    • I realized that in this assignment I probably did not need to check this place. But I will keep in mind that a separate part of the program should deal with the log. - Pavel
    • @ Pavel good. And you're doing well writing unit tests. - Nick Volynkin
    • Only TDD! only hardcore!))) - Pavel