1

I have the following classes:

class LibraryClass implements LibraryInterface {
    @Override
    public void calculate(Order order)
    {
        //some logic
    }
    @Override
    protected void findAll(Order order)
    {
     //some logic
    }

    protected Map resetAllValues(Order order)
    {
     //some logic
    }
    @Override
    protected void sum(order) {//somelogic}
}
class MyCustomClass extends LibraryClass {
    @Override
    public void calculate(Order order)
    {
        findAll(order);

    }

    @Override
    protected void findAll(Order order)
        {
            //some logic
        final Map taxValueMap = resetAllValues(order);

        sum(order);
        }

    @Override
    protected void sum(order)
    {
        int totalCount = getTotalPrice(order);
        int totalItems = getTotalItems(order);
    }
    private int getTotalPrice(Order order) { ...}
    private int getTotalItems(Order order) { ...}
}

Where LibaryClass and LibraryInterface are from a library that I cannot change and I have implemented MyCustomClass. My goal is with Mockito to verify that the values of totalCount and totalItems are some expected value when I pass in calculate method some order.

Now I got stumbled across 2 problems:

  1. resetAllValues(order) - I want to make this method return an empty map with Mockito, but I can't because the method is protected. If I don't make this method return an empty map for example it tries to make DB calls and throws exceptions.

  2. If I manage somehow to do 1., then how should I verify that the returned values from the private methods getTotalPrice and getTotalItems return the correct value.

Joel
  • 63
  • 4
  • 1
    "If I don't make this method return an empty map for example it tries to make DB calls and throws exceptions.". Where is this done? – João Dias Dec 21 '21 at 15:06
  • this is done in the resetAllValues(order) implemenentation in LibraryClass. – Joel Dec 21 '21 at 15:08
  • @xerx593 This resolved my first question, but the 2-nd remains – Joel Dec 21 '21 at 16:00
  • 2.: Normally "no one tests 'non-bulic' methods", because: they were all (ideally in all (corner) cases) covered by some "public (method) test"! ;) So you would not "verify on private", but run multiple "pulbic" (calculate()?) tests, which "discover all branches of private methods"...you would verify on the (expectation/consitency) of the main result (..with varying inputs/mocks) – xerx593 Dec 21 '21 at 16:08
  • 2: [How do you unit test private methods?](https://softwareengineering.stackexchange.com/q/100959/41957) -> "You generally don't ..." ;) – xerx593 Dec 21 '21 at 16:12

1 Answers1

-1

The creator of the library may have a reason to limit the visibility of resetAllValues() but in your case it prohibits you from applying the Composition over Inheritance pattern.

So what I would do is to create a minimal subclass of LibraryClass doing nothing but making resetAllValues() public accessible (or at least accessible in the package of MyCustomClass). This class is "too simple to fail" so that it does not need unit-tests

class MyLibraryClass extends LibraryClass {
   @override Map resetAllValues(Order order){
      return super.resetAllValues(order);
   }
}

Then I'd inject an instance of this new class into MyCustomClass:

class MyCustomClass implements LibraryInterface {
    private final MyLibraryClass delegate;
    MyCustomClass(MyLibraryClass delegate){
       this.delegate=delegate;
    }
    
    @Override
    public void calculate(Order order)
    {
        findAll(order);
    }

    @Override
    protected void findAll(Order order)
    {
        //some logic
         final Map taxValueMap = elegate.resetAllValues(order);
         sum(order);
    }

    @Override
    protected void sum(order)
    {
        int totalCount = getTotalPrice(order);
        int totalItems = getTotalItems(order);
    }
    private int getTotalPrice(Order order) { ...}
    private int getTotalItems(Order order) { ...}
}

Now Mockito can mock MyLibraryClass.resetAllValues().

Timothy Truckle
  • 15,071
  • 2
  • 27
  • 51