Skip to content
Advertisement

Mock a constructor with parameter

I have a class as below:

JavaScript

The logic in the constructor A(String test) and check() are the things I am trying to mock. I want any calls like: new A($$$any string$$$).check() returns a dummy string "test".

I tried:

JavaScript

But it doesn’t seem to be working. new A($$$any string$$$).check() is still going through the constructor logic instead of fetch the mocked object of A.

Advertisement

Answer

The code you posted works for me with the latest version of Mockito and Powermockito. Maybe you haven’t prepared A? Try this:

A.java

JavaScript

MockA.java

JavaScript

Both tests should pass with mockito 1.9.0, powermockito 1.4.12 and junit 4.8.2

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement