jeudi 6 février 2020

What is the best practice for writing tests for classes with composition

Assume I have class Formater

class Formatter {
  public FormattedData format(Map<String, Data> data) {
    return .....
  }  
}

and another class Collector that uses Formatter and can return formatted data

class Collector {
  Formatter formatter;
  Map<Id, Data> map = new HashMap<>()

  class Collector (Formatter formatter) {
    this.formatter = formatter;  
  }

  public void addData(Data data) (
    map.put(data.getId(), data);
  }

  public FormattedData getFormattedData() {
    return formatter.format(map)
  }

So the problem - I want to write tests. I wrote all the tests for Formatter class, but how should I test Collector?

Since I should not rely on implementation of collector - I need to copy all tests for Formatter and pass them as input for Collector. Surely in tests I would change Map<String, Data> data to Data data as input data type, but anyway there will be a huge code duplication. How can I avoid it?

Aucun commentaire:

Enregistrer un commentaire