1

I have the following class:

class MyClass {

  public void doIt() {
     methodOne();
     methodTwo();
     methodThree();
  }

  private void methodOne() {
     // ...
  }

  // rest of methods similar...

}

My intention is to verify that when i invoke doIt(), methods metodOne(), methodTwo() and methodThree() will be invoked, in that order.

I'm using mockito for mocking. Does anyone knows how i can test this scenario?

bric3
  • 40,072
  • 9
  • 91
  • 111
Lefteris Laskaridis
  • 2,292
  • 2
  • 24
  • 38

2 Answers2

9

Hate to be the person but: simply don't test this. Test the output, side effects, results - not the implementation.

If you really want to ensure the correct order, extract these methods into separate class(es) and mock them.

Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • The voice of sanity has spoken. Great first paragraph. If you focus on results while you test, you'll end up with far better results than if you focus on the implementation itself. – Dawood ibn Kareem Apr 11 '12 at 06:23
  • in this particular case i don't actually want to test the state. i want to test the behavior of my method (http://martinfowler.com/articles/mocksArentStubs.html) – Lefteris Laskaridis Apr 11 '12 at 07:01
  • @lefty Testing that these interactions happen, would really be better for design and maintainability if you put them in another object that you can mock. – bric3 Apr 11 '12 at 09:33
3

As long as your private methods are ... well ... private, don't test them. They are implementation details. They could be refactored without changing the contract of your class. Test the correct outcome of your public methods.

DerMike
  • 15,594
  • 13
  • 50
  • 63
  • My public method is just an init() method. It doesn't return anything, it just sets the class state. What i want to do is to verify that the initialized state is the correct one. However, since i'm extending from a class which i don't own i must stub several of its methods used internally from init() to do that (else it simply wont run). – Lefteris Laskaridis Apr 10 '12 at 17:06
  • IMHO tests exist to help you refactor code by giving you confidence that everything is still working. So if you want to ensure correct initialization check for it. (It may not be trivial on legacy code.) I think nothing is won by asserting a certain way of doing it rather than checking the result. – DerMike Apr 10 '12 at 17:19