2016-07-22 122 views
0
public class DownloadBoxHelper extends WCMUsePojo { 
private static final Logger log = LoggerFactory.getLogger(DownloadBoxHelper.class); 
private ArrayList<Map<String, String>> downloadList; 
private ArrayList<Map<String, String>> downloadListFinal; 
DownloadBoxModel downloadBoxModel; 
@Override 
public void activate() throws Exception { 
    log.info("Download Box activate Method started"); 
    JcrUtilService jcrUtil = getSlingScriptHelper().getService(JcrUtilService.class); 

    downloadBoxModel = getResource().adaptTo(DownloadBoxModel.class); 
    downloadList = downloadBoxModel.getDownloadList(); 
    downloadListFinal =DHLUtil.getSizeTypeOfAsset(downloadList, getResource(), jcrUtil); 
    downloadBoxModel.setDownloadListFinal(downloadListFinal); 
    log.info("Download Box activate Method Ended"); 
} 

public DownloadBoxModel getDownloadBoxModel() { 

    return downloadBoxModel; 
} 

}如何使用powermock模擬靜態方法?

我想嘲笑這個輔助類。但是這個幫助類有一些靜態方法,如downloadListFinal = DHLUtil.getSizeTypeOfAsset(downloadList,getResource(),jcrUtil); 此靜態方法引用DHLUtil.class文件。這是聲明

**public static ArrayList<Map<String, String>> getSizeTypeOfAsset(ArrayList<Map<String, String>> downloadList, 
     Resource rs, JcrUtilService jcrUtil) { 
    log.info("DHLUtil getSizeTypeOfAsset() initiated "); 
    ArrayList<Map<String, String>> localDownloadList = new ArrayList<Map<String, String>>(); 
    Session session = null; 
    Node assetMetaNode; 
    try { 
     session = jcrUtil.getSession(DHLSubService.readservice); 
     Iterator<Map<String, String>> it = downloadList.iterator(); 
     while (it.hasNext()) { 
      Map<String, String> mp = it.next(); 
      if (mp.get(DHLConstants.ASSET_DOWNLOAD_ITEM).toString().contains(".")) { 
       assetMetaNode = session.getNode((mp.get(DHLConstants.ASSET_DOWNLOAD_ITEM).toString()) 
         + DHLConstants.SLASH + JcrConstants.JCR_CONTENT +DHLConstants.SLASH + DamConstants.ACTIVITY_TYPE_METADATA); 
       String assetType = assetMetaNode.getProperty(DamConstants.DC_FORMAT).getString(); 
       if(assetType!=null){ 
        if(assetType.contains("vnd.openxmlformats-officedocument.spreadsheetml.sheet") || assetType.contains("vnd.ms-excel")){ 
         assetType="ms-excel"; 
        } 
        if(assetType.contains("vnd.openxmlformats-officedocument.wordprocessingml.document") || assetType.contains("msword")){ 
         assetType="ms-word"; 
        } 
        if(assetType.contains("vnd.openxmlformats-officedocument.presentationml.presentation") || assetType.contains("vnd.ms-powerpoint")){ 
         assetType="ms-powerpoint"; 
        } 

       } 

       Property assetSize = assetMetaNode.getProperty(DamConstants.DAM_SIZE); 
       double assetSizeUpdated = 0d; 
       DecimalFormat df = new DecimalFormat("0.0"); 
       String assetSizeType = DHLConstants.BYTE; 
       ; 
       if (assetSize.getLong() < (1024)) { 
        assetSizeUpdated = (double) assetSize.getLong(); 
       } 
       if (assetSize.getLong() > 1024 && assetSize.getLong() < (1024 * 1024)) { 
        assetSizeType = DHLConstants.KILOBYTE; 
        assetSizeUpdated = (double) assetSize.getLong()/1024L; 
       } 
       if (assetSize.getLong() > (1024 * 1024)) { 
        assetSizeType = DHLConstants.MEGABYTE; 
        assetSizeUpdated = ((double) assetSize.getLong()/(1024 * 1024)); 
       } 
       if (assetType.contains("/")) { 
        String strSplit[] = assetType.split("/"); 
        assetType = strSplit[1]; 
       } 
       String strMetaData = assetType.toUpperCase() + DHLConstants.SPACE + DHLConstants.LEFT_BRACKET 
         + DHLConstants.SPACE + df.format(assetSizeUpdated) + DHLConstants.SPACE + assetSizeType + DHLConstants.SPACE + DHLConstants.RIGHT_BRACKET; 
       mp.put(DamConstants.ACTIVITY_TYPE_METADATA, strMetaData); 
       localDownloadList.add(mp); 
      } 
     } 
    }catch (DHLException dhe) { 
     log.error("DHLException {}", dhe); 
    }catch (Exception e) { 
     log.error("Exception {}", e); 
    }finally { 
     if(null!=session && session.isLive()) { 
      session.logout(); 
     } 
    } 

    return localDownloadList; 
} 

那麼我怎麼嘲笑這個?

我JUnit的文件是:

**@RunWith(PowerMockRunner.class) 
    @PrepareForTes({DownloadBoxHelper.class,DHLUtil.class,DownloadBoxModel.class}) 

public class DownloadBoxHelperTest extends PowerMockTestCase { 
    private DownloadBoxHelper aFinalClass_mock = null; 

    @Test 
     public void mockFinalClassTest() { 
     ArrayList<Map<String, String>> downloadList = new ArrayList<Map<String, String>>();; 
     ArrayList<Map<String, String>> downloadListFinal; 
     Map<String, String> n = new HashMap<String, String>(); 
     n.put("a", "a"); 
     n.put("b", "b"); 
     downloadList.add(n); 

      DownloadBoxModel downloadBoxModel; 

      aFinalClass_mock = PowerMockito.mock(DownloadBoxHelper.class); 
      Mockito.when(aFinalClass_mock.getSlingScriptHelper()).thenReturn(null); 

      // Assert the mocked result is returned from method call 
      //Assert.assertEquals(aFinalClass_mock.getSlingScriptHelper()).thenReturn(null); 
     } 

    @Test 
     public void mockFinalClassTest_1() { 
     JcrUtilService jcrUtil;s 
     ArrayList<Map<String, String>> downloadListFinal; 
     Map<String, String> n1 = new HashMap<String, String>(); 
     n1.put("a", "a"); 
     n1.put("b", "b"); 
     downloadListFinal.add(n1); 


     Mockito.when(aFinalClass_mock.getDownloadListFinal()).thenReturn(downloadListFinal); 
      // Assert the mocked result is returned from method call 
      //Assert.assertEquals(aFinalClass_mock.getSizeTypeOfAsset(downloadListFinal, getResource(), jcrUtil);, mockedResult); 
     } 

,我們正在使用[」

public static ArrayList<Map<String, String>> getSizeTypeOfAsset(ArrayList<Map<String, String>> downloadList, 
    Resource rs, JcrUtilService jcrUtil) { 
log.info("DHLUtil getSizeTypeOfAsset() initiated "); " 

]這種類型的類

感謝

請提供給我的解決方案或一個參考JUnit的文件
+3

問「儘快給我提供的解決方案」,是不是很客氣;而最初的「請」在這裏並沒有真正的幫助。而且你知道,你在我們身上傾銷了這麼多醜陋的格式化代碼...並不完全適用。換句話說:請轉到幫助中心,看看如何提出「不工作」的問題;然後嘗試構建一個**最小**可行的示例來顯示您的問題。不要向我們扔這麼多的代碼,並儘快結果。 – GhostCat

+1

只是爲了它:使用** static **方法只是簡單地讓你的代碼難以測試。雖然PowerMock可能看起來像是對這個問題的正確答案......但事實並非如此。有機會,你會花很多時間來尋找奇怪的Powermock問題......所以,至少考慮相反的事情。含義:將您的生產代碼重新設計爲** testable **;然後避免使用Powermock。 – GhostCat

+0

請在提問之前檢查文檔https://github.com/jayway/powermock/wiki/MockitoUsage#mocking-static-method –

回答

3

您應該添加:

PowerMockito.mockStatic(DHLUtil.class); 

和您可以使用此方法像任何其他模擬:

when(DHLUtil.getSizeTypeOfAsset()).thenReturn(whatever); 
相關問題