jeudi 4 mai 2017

Test class with several method object

I have a class whith some complex logic, so I decide to use the use method object several times to refactoring it .

Suppose a class like next:

public class MapperClass {

    Entity entity = new Entity(); 
    private final Item item;

    public MapperClass(Item item) {
       this.item = item;
    }

    public Entity getEntity() {
          entity.setPrice(extractPrice());
         //more long and complicated implementation
         //mapped from item values
         //entity.set....
         //...
         return entity;
    }

    private double extractPrice() {
        double priceExtracted = 0d;
        //Long and complicated implementation
        //extracted from item
        //...
        return priceExtracted ;
    }

    private List<SubItem> getSubItemsMapped() {
       //Long and complicated implementation
       //extracted from item
       //...
    }

}

I refactor it to:

 public class MapperClass{

    private Entity entity = new Entity(); 
    private final Item item;

    public MapperClass(Item item) {
       this.item = item;
    }

    public Entity getEntity() {
          entity.setPrice(extractPrice());
          entity.setSubEntities(getSubItemsMapped());
         //more long and complicated implementation
         //mapped from item values
         //entity.set....
         //...
         return entity;
    }

    private double extractPrice() {
        new PriceCalculator(item).getPrice();
    }

    private List<SubItem> getSubItemsMapped() {
       new SubItemsMapper(item).getSubItems();
    }

   //More similar cases
}

So then I have a class whith several method objects which is cleaner than the one complicated class.

Then when I go to do my tests, I've got the doubt how to test this class.

Which is the best way to test a class with a method object?

  • Test only the class with the method objects
  • Test all classes, method object classes independently and then the class with method objects, all in deep.
  • Test all classes, but the method objects classes in deep, and the class with method objects with a simple class to check it is at least mapped by this method objects.
  • It has a bad design so rethink how to design it, and then test it.
  • Other options.

What is usually done in these cases?

Aucun commentaire:

Enregistrer un commentaire