proutray
proutray

Reputation: 2033

How do I mock an object of a class with parameterized constructor?

For the code

public class A{
    public A (B b, C c){
    //do something here
    }
}

For testing, I wanted to create a mock object. What I am doing now is

B bmock = mock(B);
C cmock = mock(C);
A aobject = new A(bmock, cmock);

However, this doesn't allow me call verify() on aobject as it is not mocked. How to do that?

Upvotes: 0

Views: 1553

Answers (1)

Mathias Dpunkt
Mathias Dpunkt

Reputation: 12184

You could use a Spy:

A aobject = spy(new A(bmock, cmock));

So you are actually calling the implementation of A but can still verify interactions.

See the doc for details: http://site.mockito.org/mockito/docs/current/org/mockito/Mockito.html#spy(T)

Upvotes: 4

Related Questions