2017-09-25 84 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只有在单位*行为*改变以便在重构期间成为安全警卫时才应该刹车。 –

相关问题