2017-09-21 53 views
0

喜逢一個我試圖嘲弄一個靜態方法名mapCreditInfo(UCIPin,creditAssessmentResults)它有兩個參數UCIPincreditAssessmentResults。 UCIPin是字符串類型和creditAssessmentResults是列表 此方法是一個公共類ResponseMapper 類型內,如下所示:如何在使用Power mockito的公共類中模擬靜態方法?

private CreditInfo getAccountByUCI(String audiUser, String UCIPin) throws EnterpriseCustomerVerificationException { 
    List<CreditAssessmentResult> creditAssessmentResults = creditInfoRepository.getUCISummary(UCIPin, audiUser); 
    return ResponseMapper.mapCreditInfo(UCIPin, creditAssessmentResults); 
} 

Note: getAccountbyUCI method is called inside another public method name executeCustomerVerification which is in the class EnterpriseCustomerVerificationService

ResponseMapper類

​​

}

我已經像我的測試課的一部分一樣試過了hown下面: 測試類

@PrepareForTest(EnterpriseCustomerVerificationService.class) 
@RunWith(PowerMockRunner.class) 
public class EnterpriseCustomerVerificationServiceTest { 

@InjectMocks 
    private EnterpriseCustomerVerificationService enterpriseCustormerVerificationServiceMock ; 
@Test 
public void executeCustomerVerificationTest() throws Exception { 
    List<ErrorResponse> errorResponses = getErrorResponse(); 
    List<String> mso = new ArrayList<String>(); 
    mso.add("a"); 
    mso.add("b"); 
    mso.add("c"); 
    AddressResponse addressResponse = getAddressResponse(); 
    String experianAuthorization = "experianAuthorization"; 
    String UCIPin = "110019"; 
    String auditUser = "ABC"; 

    CreditInfo credit = getCreditInfo(); 
    CreditCheck creditCheck = getcreditCheck(); 
    EnterpriseCustomerVerificationService spy = PowerMockito.spy(new EnterpriseCustomerVerificationService()); 

    PowerMockito.when(spy,PowerMockito.method(EnterpriseCustomerVerificationService.class,"executeCreditCheck",CreditCheck.class)).withArguments(Mockito.any()).thenReturn("@1"); 
    Mockito.when(creditInfoRepository.getUCISummary("110019", "test")).thenReturn(getCreditAssessmentResultList()); 

    PowerMockito.mockStatic(ResponseMapper.class); 
    Mockito.when(ResponseMapper.mapCreditInfo(UCIPin, getCreditAssessmentResultList())).thenReturn(credit); 

    CustomerVerification cv = spy 
      .executeCustomerVerification(getCustomerVerificationRequest1(), 
        "101"); 
} 

我的問題是如何嘲笑使用電力的Mockito 靜態mapCreditInfo方法?

感謝

+0

評論你能告訴我們你有什麼到目前爲止已經試過?你已經寫了一些試圖嘲笑這種靜態方法的測試方法,所以也許你可以更新問題來包含該嘗試? – glytching

+0

ok等待@ glitch –

+0

@ glitch請檢查 –

回答

1

就像這個...

@RunWith(PowerMockRunner.class) 
@PrepareForTest({ResponseMapper.class}) 
public class ATest { 

    @Test 
    public void testMockingStatic() { 
     PowerMockito.mockStatic(ResponseMapper.class); 

     // if you want to use specific argument matchers 
     Mockito.when(ResponseMapper.mapCreditInfo(
      uciPin, creditAssessmentResults) 
     ).thenReturn(creditInfo); 

     // or if you want to match on any arguments passed into your static method ... 
     Mockito.when(ResponseMapper.mapCreditInfo(
      ArgumentMatchers.anyString(), 
      ArgumentMatchers.anyList()) 
     ).thenReturn(creditInfo); 

     // ... 
    } 
} 

注:

  • @PrepareForTest準備類要嘲笑
  • PowerMockito.mockStatic嘲笑所有靜態方法靜態方法該類別
  • You ca n使用標準的Mockito時/然後構造告訴嘲笑靜態方法是什麼返回
  • 上面的例子使用了這些依賴關係:
    • org.mockito:mockito-core:2.7.19
    • org.powermock:powermock-module-junit4:1.7.0
    • org.powermock:powermock-api-mockito2:1.7.0

更新1:基於您更新的問題,它顯示您的測試方法...

我的例子包括:@PrepareForTest({ResponseMapper.class})測試方法準備ResponseMapper相反,它正準備EnterpriseCustomerVerificationService。這就像你正在準備調用具有靜態方法的類的類,而不是準備包含靜態方法的類。

我強烈建議創建一個新的測試用例 - 只是暫時的 - 這看起來就像我提供的那個,用它來展示如何模擬一種靜態方法,一旦你對此感到滿意,然後將它工作到你的EnterpriseCustomerVerificationServiceTest

+0

我已經嘗試過它,但它給出了以下錯誤:org.mockito.exceptions.misusing.MissingMethodInvocationException: when()需要一個必須是'模擬方法調用'的參數。 例如: when(mock.getArticles())。thenReturn(articles); –

+0

該消息告訴你,你傳遞給'Mockito.when()'的參數是**而不是**模擬。我建議從我提供的骨架開始,它的工作原理。 – glytching

+0

此外,可能會顯示此錯誤,因爲: 1.您存根:final/private/equals()/ hashCode()方法中的任一個。 這些方法*不能*被存根/驗證。 不支持在非公共父類上聲明的模仿方法。 2.內部when()你不會在模擬上調用方法,而是在某個其他對象上。這是什麼意思 ? @glitch –

相關問題