How do I mock the constructor of an object?

24 views
Skip to first unread message

Antony Raj

unread,
Nov 17, 2014, 3:41:58 PM11/17/14
to mockito...@googlegroups.com
When I call a method of object x, it instantiates an another object there. I want to mock it out.
I tried in the following way.
when(ProductFunction).__init__(ANY()).thenReturn(product_function)

It doesn't work. Can any of you offer me a help please?

Justin Hopper

unread,
Nov 18, 2014, 1:56:18 AM11/18/14
to mockito...@googlegroups.com
Hello Antony,

Can you provide a bit more context so I can better assist you? Such as the test code leading up to this call and the production code or declarations for the ProductFunction.

Is there a reason you can't mock the Class Production function and allow the function mentioned in your question to return an instance?

For example:
when(ProductFunction).a_func(ANY()).thenReturn("test value")

object_x
.call_product_function()
# some internal call to production function
Reply all
Reply to author
Forward
0 new messages