mercredi 13 avril 2016

Mock out class nested inside classes for testing

class Alpha {
  Beta beta;

  void start() {
    beta = new Beta();
  }
}

class Beta {
  Charlie charlie;

  Beta() {
    charlie = createCharlie().randomize();
  }

  Charlie createCharlie() {
    return new Charlie();
  }
}

Alpha alpha = new Alpha();
alpha.start();

For testing, I would like to supply my own Charlie. How should I set it up?

I am not sure, I tried a bunch of ways (mocking out createCharlie() function, mocking out Charlie class, mocking out randomize(), moving createCharlie() to parent Alpha class, nothing really works properly or maybe I was missing something. Any help would be really appreciated. Thanks!

Aucun commentaire:

Enregistrer un commentaire