1

Does anybody know of a way in powermock you can unit test a constructor and mock the methods that the constructor calls.

i.e. I have a class like this;

public class myClass {
    public myClass(){
        myMethod();
        // do other stuff
    }
    public void myMethod(){
        // do stuff
    }
}

What I want to do is write a unit test for myClass() constructor that mocks myMethod().

This would be easy if myMethod() was static as I could use mockStaticPartial() then invoke the constructor.

Just creating a partial mock of MyClass won't work either as once I've created the mock I've created it, invoking the constructor at that point will just create a new instance of MyClass without myMethod() being mocked.

Anyone know of any ways?

mpc
  • 79
  • 11
  • you must have started TDD late in your project... – user1329572 Sep 13 '12 at 15:00
  • 3
    By the way, it is bad form to call a public non-final method from your constructor. Make the method private or final. http://stackoverflow.com/questions/3404301/whats-wrong-with-overridable-method-calls-in-constructors – John Ericksen Sep 13 '12 at 15:05
  • Ok lets say I do make the method called from the constructor private, I still would like to mock it. My example was just a quick bit of code I put together to illustrate what I'm trying to do - mock a method called from a constructor using powermock – mpc Sep 17 '12 at 07:50

1 Answers1

2

You should not mock a class under test. If the constructor calls a method then the actions of the method should be tested as part of testing the constructor. If the method does stuff that is outside the scope of unit testing, mock the classes that method is using to do the "stuff".

If you REALLY want to do it with what you have above, (I don't recommend this) you could create a sub-class of your class under test that overrides the method. (this goes against johncarl's excellent comment above).

John B
  • 32,493
  • 6
  • 77
  • 98
  • With powermock it's perfectly reasonable to partially mock the class under test, this is in fact one of powermocks main advantages. Creating a subclass and overriding methods is in fact what powermock does when creating partial mocks, this still leaves me with the problems described in my question. – mpc Sep 17 '12 at 07:54
  • The problem that you face is that the mocking frameworks create a wrapping proxy. However, once inside the mocked instance, when it calls one of its own methods it does not exit and class a reentry thereby passing through the proxy. Hence even with Mockito's spy mechanism which allows stubbing an instance method, this would not work since the method to be stubbed is called from within the instance being spied. I reiterate the subclass and override solution. – John B Sep 17 '12 at 12:50
  • 1
    Hmm I guess I was hoping that there'd be some way with powermock's class loader to mock out the method call in the constructor before you create the object itself. Perhaps you're right and overidding the method myself is the only way forward. Suppose it's something that just can't be done in power mock, thanks for your help! :-) – mpc Sep 17 '12 at 14:40