junit - Not able to mock constructor using PowerMock -
here in below code not able mock constructor using powermock. want mock below statement.
apspportletrequest wrappedrequest = new apspportletrequest(request);
below mocking steps
@preparefortest({apspportletrequest.class}) @runwith(powermockrunner.class) public class reminderportletcontrollertest { private portletrequest requestmock; private apspportletrequest apspportletrequestmock; public void setup() throws exception { requestmock = easymock.createnicemock(portletrequest.class); apspportletrequestmock = easymock.createnicemock(apspportletrequest.class); } @test public void testexecutemethod() throws exception { powermock.expectnew(apspportletrequest.class, requestmock).andreturn(apspportletrequestmock).anytimes(); easymock.replay(apspportletrequestmock, requestmock); powermock.replayall(); } }
please suggest me on that.
as want mock line
apspportletrequest wrappedrequest = new apspportletrequest(request);
this object creation call takes 1 parameter,but while mocking in test method passing 2 values expectnew
method.
actually should doing
powermock.expectnew(apspportletrequest.class, easymock.anyobject(requestclass.class)).andreturn(apspportletrequestmock).anytimes();
by doing telling compiler return mocked instance apspportletrequestmock whenever 'new' operator called on class apspportletrequest object of request class parameter.
and missing small point need replay easymock objects too.. i.e. easymock.replay(...);
needs present.
hope helps!
good luck!