在if语句中调用方法时,Mockito Null指针异常

时间:2019-10-24 07:10:45

标签: nullpointerexception mockito junit4

我们正在尝试为下面的类编写JUnit,我不知道为什么会得到空指针异常,我们正在使用Mockito框架进行模拟,看来一切正确,但仍然会出现NullPointer异常。

public class DefaultExtCMSProductBuyableService implements ExtCMSProductBuyableService {

  private static final Logger LOG = LoggerFactory.getLogger(DefaultExtCMSProductBuyableService.class);

  private AvailabilityService availabilityService;
  private ProductService productService;
  private BSHBaseSiteService bshBaseSiteService;
  private ContextService contextService;
  private BSHUrlGeneratorFacade bshUrlGeneratorFacade;

  @CustomContext
  @Override
  public boolean validateProductListPurchasability(final List<String> productCodes, final String catalogTypeCode) {
        if (CollectionUtils.isEmpty(productCodes) || StringUtils.isEmpty(catalogTypeCode)) {
              LOG.warn("Product codes or Catalog type is null, can not check purchasability. Products --> {}, Catalog Type --> {}",
                          productCodes, catalogTypeCode);
              return false;
        }
        final CatalogModel productCatalog = bshBaseSiteService
              .getProductCatalogForCurrentSite(BSHProductCatalogType.valueOf(catalogTypeCode));
        if (productCatalog == null) {
              return false;
        }

        contextService.enableProductCatalog(productCatalog);

        for (final String productCode : productCodes) {
              final StockValue stockValue = availabilityService
                    .getStockValueForCurrentCatalog(productService.getProductForCode(productCode));

              final ProductModel product = productService.getProductForCode(productCode);
              if (!stockValue.isBuyable() || !isValidAgent(product)) {
                    LOG.warn("Given product {} is not buyable and given user {} is not a salesagent,hence returning false.",
                                productCode, contextService.getCurrentUser());
                    return false;
              }
        }
        return true;
  }

protected boolean isValidAgent(final ProductModel productModel) {
    if (contextService.getCurrentUser() instanceof BSHSalesAgentModel
            && productModel instanceof BSHVibProductModel) {
        final BSHSalesAgentModel salesAgent = (BSHSalesAgentModel) contextService.getCurrentUser();
        final BSHVibProductModel product = (BSHVibProductModel) productModel;

        if (!salesAgent.getBrands().contains(product.getBrand())) {
            return false;
        }
    }
    return true;
}

}

这是我们的测试班

@UnitTest

@RunWith(MockitoJUnitRunner.class) 公共类DefaultExtCMSProductBuyableServiceTest {

@InjectMocks
private final DefaultExtCMSProductBuyableService systemUnderTest = new DefaultExtCMSProductBuyableService();

@Mock
private AvailabilityService availabilityService;
@Mock
private ProductService productService;
@Mock
private BSHBaseSiteService bshBaseSiteService;
@Mock
private ContextService contextService;
@Mock
private BSHUrlGeneratorFacade bshUrlGeneratorFacade;

private final List<String> productCodes = new ArrayList<>();
private String catalogTypeCode;
private CatalogModel productCatalog;
private StockValue buyableStock;
private StockValue notBuyableStock;
private BSHSalesAgentModel SalesAgent;
private BSHConsumerModel notSalesAgent;
private BSHVibProductModel vibProduct;
private ProductModel Product;
private static final String PRODUCT1 = "Product1";
private static final String PRODUCT2 = "Product2";

@Before
public void setup() {
    productCatalog = Mockito.mock(CatalogModel.class);
    buyableStock = Mockito.mock(StockValue.class);
    notBuyableStock = Mockito.mock(StockValue.class);

    Mockito.when(productService.getProductForCode(Matchers.anyString()))
            .thenReturn(Mockito.mock(ProductModel.class));

    Mockito.when(notBuyableStock.isBuyable()).thenReturn(false);
    Mockito.when(buyableStock.isBuyable()).thenReturn(true);
    Mockito.when(contextService.getCurrentUser()).thenReturn(Mockito.mock(UserModel.class));
    SalesAgent = Mockito.mock(BSHSalesAgentModel.class);
    notSalesAgent = Mockito.mock(BSHConsumerModel.class);
    vibProduct = Mockito.mock(BSHVibProductModel.class);
    Product = Mockito.mock(ProductModel.class);
}


@Test
public void productsPurchasePurchasabilityTestWhenValidAgent() {
    setProductCodes();
    setCatalogType();

    Mockito.when(bshBaseSiteService.getProductCatalogForCurrentSite(BSHProductCatalogType.SHOP))
            .thenReturn(productCatalog);
    Mockito.when(availabilityService.getStockValueForCurrentCatalog(productService.getProductForCode(PRODUCT1)))
            .thenReturn(buyableStock);
    Mockito.when(availabilityService.getStockValueForCurrentCatalog(productService.getProductForCode(PRODUCT2)))
            .thenReturn(buyableStock);
    //Mockito.when(contextService.getCurrentUser()).thenReturn(SalesAgent);
    final BSHSalesAgentModel user = Mockito.mock(BSHSalesAgentModel.class);
    Mockito.when(contextService.getCurrentUser()).thenReturn(user);
    Mockito.when(productService.getProductForCode(Matchers.anyString())).thenReturn(vibProduct);
    final boolean result = systemUnderTest.validateProductListPurchasability(productCodes, catalogTypeCode);
    Assert.assertTrue(result);
}

private void setProductCodes() {
    productCodes.add(PRODUCT1);
    productCodes.add(PRODUCT2);
}

private void setCatalogType() {
    catalogTypeCode = "SHOP";
}

}

0 个答案:

没有答案