2015-04-23 74 views
1

在下面的代碼中,我無法使用PowerMock模擬構造函數。 我想要MOK以下語句。無法使用PowerMock模擬構造函數

APSPPortletRequest wrappedRequest = new APSPPortletRequest(request); 
下面

是我的嘲諷步驟

@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(); 
    } 
} 

請建議我上。

+0

您能否提供錯誤或某些提示,指出確切問題是什麼 – Pratik

回答

1

只要你想嘲笑這一行

APSPPortletRequest wrappedRequest = new APSPPortletRequest(request); 

這個對象創建調用只需要一個參數,但在嘲笑你的測試方法,則需要傳遞兩個值expectNew方法。

其實你應該這樣做,你是在告訴編譯器做

PowerMock.expectNew(APSPPortletRequest.class, EasyMock.anyObject(requestClass.class)).andReturn(apspPortletRequestMock).anyTimes(); 

返回一個嘲笑實例apspPortletRequestMock只要「新」運算符稱爲階級APSPPortletRequest與請求類的任何對象作爲參數。

而且您還錯過了一小部分,您需要重播所有的Easymock對象..即EasyMock.replay(...);也需要存在。

希望這有助於!

祝你好運!

0

如果你想嘲笑以下方法:

EncryptionHelper encryptionhelper =新EncryptionHelper( 「CEP」,真正的);

你可以用powerMock這樣做。

1.導入類。

import static org.powermock.api.support.membermodification.MemberMatcher.method;

import static org.powermock.api.support.membermodification.MemberModifier.stub;

2.添加註釋@RunWith和@PrepareForTest在你上面junit測試calss。

@RunWith(PowerMockRunner.class)

@PrepareForTest({EncryptionHelper.class})

3.Mock它。

EncryptionHelper encryptionHelperMock = PowerMock.createMock(EncryptionHelper.class); PowerMock.expectNew(EncryptionHelper.class,isA(String.class),EasyMock.anyBoolean())andReturn(encryptionHelperMock);

4。回覆它

PowerMock.replayAll(encryptionHelperMock);

我這樣做與上述步驟,並正常工作。

相關問題