PowerMockPolicy - Class Construction

12 views
Skip to first unread message

ron.difrango

unread,
Feb 21, 2012, 11:25:36 AM2/21/12
to PowerMock
Is there a way when you implement a PowerMockPolicy to provide a mock
policy for the construction of a new object? As far as I can tell,
you can only provide mocks for methods and constructor's are treated
as methods. Thoughts on a way to approach this?

Johan Haleby

unread,
Feb 21, 2012, 1:51:11 PM2/21/12
to powe...@googlegroups.com
Unfortunately you can't mock constructors from a MockPolicy but I don't
think it would be too hard to implement so if you're up for it feel free
to create a patch :)

/Johan

ron.difrango

unread,
Feb 21, 2012, 1:55:13 PM2/21/12
to PowerMock
I'm definitely looking into that...though I'm not 100% sure I can do
accomplish :-)

Actually, I'm looking into a way to auto-wire mocks for classes that
are under test that have the JSR-330 @Inject annotation.

Johan Haleby

unread,
Feb 21, 2012, 2:06:46 PM2/21/12
to powe...@googlegroups.com
Are you looking for a way to automatically create mocks and inject them
to fields annotated with @Inject? In this case I would rather write a
JUnit rule and then use Whitebox.getAllFieldsAnnotatedWith(..) and for
each of those fields I would create a mock instance and set it to the field.

/Johan

ron.difrango

unread,
Feb 21, 2012, 3:14:34 PM2/21/12
to PowerMock
Never thought of that...that might just work. I'll have to look into
it.
Reply all
Reply to author
Forward
0 new messages