jeudi 26 novembre 2015

Testing wrapper class

I would like to test a class that is used as a wrapper that provides abstraction for the classes underneath.

That means that I need to create some objects inside that class, that is, I don't pass collaborators through constructor or some method.

For example, here's a wrapper class:

public class Abstraction {

      private ComplexClass complex;

      public class Abstraction(some parameters) {
           complex = new Complex(parameters);
      }

}

To be able to test this class I had an idea to create a method that will return an object of Complex class, like this:

public class Abstraction {

      private ComplexClass complex;

      public class Abstraction(parameters) {
           complex = createComplex(parameters);
      }

      protected createComplex ComplexClass(parameters) {
           return new ComplexClass(parameters);
      }
}

The thing is, I cannot use Mockito to mock collaborators created inside constructor of class that I'm testing. How to overcome this?

My idea was to spy the SUT so that createComplex returns mock object, but I cannot do that because SUT needs to be created first. It's kind of a dead lock.

My final goal is to test whether some methods of collaborator classes were called.

Aucun commentaire:

Enregistrer un commentaire