2014-12-22 37 views
0

我嘗試使用功率嘲諷來測試我的課即使它在語法上正確,爲什麼這個powermock不起作用?

這是我的課:

public Basket createBasket(Basket basket) { 
     if (LOGGER.isInfoEnabled()) { 
      LOGGER.info("In BasketInformationServiceImpl.createBasket : [" + basket.toString() + "]"); 
     } 
     try { 
      if (StringUtils.isBlank(basket.getBasketAdditionsAllowedInd())) { 
       basket.setBasketAdditionsAllowedInd("Y"); 
      } 

這是我的測試:

public void testCreateBasketCallsSetBasketAdditionsAllowedIndInBasket(){ 
     PowerMockito.mockStatic(StringUtils.class); 
     when(StringUtils.isBlank(mockBasket.getBasketAdditionsAllowedInd())).thenReturn(true); 
     basketInformationServiceImpl.createBasket(mockBasket); 
     verify(mockBasket,times(1)).setBasketAdditionsAllowedInd(anyString()); 
    } 

和我得到的錯誤是:

org.mockito.exceptions.missing.WrongTypeOfReturnValue; 
Boolean cannot be returned by getBasketAdditionsAllowedInd() 
getBasketAdditionsAllowedInd() should return string 

沒有人知道爲什麼我得到這個錯誤以及如何結束來吧?

p.s.使用功能模擬爲StringUtils是一個靜態類

回答

0

你有沒有嘲笑mockBasket.getBasketAdditionsAllowedInd其他地方?

如果是這樣,請嘗試將違規行分成兩部分。

String allowedInd = mockBasket.getBasketAdditionsAllowedInd(); 
when(StringUtils.isBlank(allowedInd)).thenReturn(true); 

爲什麼你嘲笑靜態類呢?