samedi 25 août 2018

How do I test the following class with unit tests?

I have the following class:

public class BookProcessor {

    private Map<String, Double> bookToValueRecord = new HashMap<>();
    private Map<String, Integer> bookToCountRecord = new HashMap<>();
    private int counter = 0;

    public void processBook(Book book) {
        counter++;
        recordBook(book);
        if (counter % 5 == 0) {
            printSomeReport();
        } if (counter % 10 == 0) {
            printAnotherReport();
        }
    }

    private void printAnotherReport() {
       //just print something
    }


    private void printSomeReport() {
        //print something else here
    }

    private void recordBook(Book book) {
        bookToValueRecord.put(book.getTitle(), book.getValue());
        bookToCountRecord.put(book.getTitle(), bookToCountRecord.getOrDefault(book.getProduct(), 0)+book.getCount());
    }

My idea was to create a Test class BookProcessorTest with field BookProcessor testee; I dont know if I need to mock it or not? One of my tests will be shouldProcessBook(). In this test I will create a sample Book object, then I will call testee.processBook(myBookObject), but then what should I assert or verify? Should I check if this record has been saved into bookToValueRecord? If yes, how, given that this is a private field? Also, how to test that after saving 5 books into my Map, I am printing some report? Should I test that printSomeReport() has been called? If yes, how, given that printSomeReport is private method?

Aucun commentaire:

Enregistrer un commentaire