2017-09-25 85 views
0

大家好我試圖嘲弄它以列表類型作爲參數的代碼細節有些部分私有方法如下:如何模擬一個私有方法,它使用功率模擬將List類型作爲參數?

CustomerVerification類

public class CustomerVerification{ 

creditCheck.setSuffix(null); 
     String pinAndPreciseId = null; 
     try { 
      pinAndPreciseId = executeCreditCheck(creditCheck, errorResponses, transactionId); 
      if (pinAndPreciseId.contains("Error")) { 
       ErrorResponse errorResponse = new ErrorResponse("EXPERIAN", pinAndPreciseId, "E01", transactionId); 
       errorResponses.add(errorResponse); 
       customerVerification.setErrorResponses(errorResponses); 
       return customerVerification; 
      } 
     } catch (Exception e) { 
      throw new EnterpriseCustomerVerificationException(e.getMessage()); 
     } 

     } 

executeCriditCheck類

private String executeCreditCheck(CreditCheck creditCheck, List<ErrorResponse> errorResponses, String transactionId) 
      throws UnsupportedOperationException, IOException { 
     LOG.info("Calling experian"); 
     String pinAndPreciseId = null; 
     String request = null; 
     String referenceId = null; 
     DateFormat formatter = new SimpleDateFormat("yyyyMMdd"); 
     Date today = new Date(); 
     referenceId = formatter.format(today); 

     HttpPost experianHttpPost = getExperianHttpPostMethod(); 
     NetConnectRequest netConnectRequest = ExperianCreditMapper.map(creditCheck, eai, dbHost, referenceId, 
       experianProduct, opInitials, preamble, subCode, arfVersion, venderNumber, vendorVersion, null, 
       CustomRRDashKeyword); 
     System.err.println("REQUEST -- " + netConnectRequest.toString()); 
     request = "NETCONNECT_TRANSACTION=" + URLEncoder.encode(netConnectRequest.toString()); 

     experianHttpPost.setEntity(new ByteArrayEntity(request.getBytes("UTF-8"))); 
     HttpResponse response = experianHttpClient.execute(experianHttpPost); 
     pinAndPreciseId = ExperianCreditMapper.getPIN(response); 
     return pinAndPreciseId; 
    } 

有人能給我一個關於如何模擬的想法私人方法executeCreditCheck它取3個參數,其中一個是List類型。

注:我在這裏只給出了部分代碼。請給我一個關於如何模擬executeCreditCheck方法的想法。

+0

A)你會看看在這裏使用間諜。 B)基本上你在這裏有難以測試的代碼。而不是以某種方式測試,而是退一步,想出改進設計的方法......以便測試變得更加容易。 – GhostCat

回答

1

對天氣測試私有方法或測試方法存在巨大爭議。就我個人而言,我不直接測試私有方法。會有公共方法調用私有方法,我更喜歡間接測試,我可能做錯了。

回到你的問題,

可以使用powermock Whitebox.invokeMethod()用於測試的私有方法。它的語法是

WhiteboxImpl.invokeMethod(<class object>, 「<Name of the private Method>",input param1, input param2,…); 
+1

我支持你不要測試私有方法,因爲這個測試通常會在*重構期間中斷*,在那裏你修改*實現*而不會改變行爲。但是UnitTests只有在單位*行爲*改變以便在重構期間成爲安全警衛時才應該剎車。 –

相關問題