I am trying to mock convertors method using 'when---thenReturn' In my serviceTest file but still its throwing null pointer exception. Is there any good way to mock convertor? Thanks in advance.
For examples, you can refer to OOTB Test classes. (Note: The classes uses given-willReturn, but you can adjust it for when-thenReturn. These are just different approaches to testing using Mockito)
If you are looking for a Converter testing its attribute values, check de.hybris.platform.commercefacades.user.converters.populator.AddressPopulatorTest
.
@UnitTest
public class AddressPopulatorTest
{
private AbstractPopulatingConverter<AddressModel, AddressData> addressConverter;
private final AddressPopulator addressPopulator = new AddressPopulator();
@Mock
private Map<String, Converter<AddressModel, StringBuilder>> addressFormatConverterMap;
@Mock
private Converter<AddressModel, StringBuilder> defaultAddressFormatConverter;
@Before
public void setUp()
{
MockitoAnnotations.initMocks(this);
addressPopulator.setAddressFormatConverterMap(addressFormatConverterMap);
addressPopulator.setDefaultAddressFormatConverter(defaultAddressFormatConverter);
addressConverter = new ConverterFactory<AddressModel, AddressData, AddressPopulator>().create(AddressData.class,
addressPopulator);
}
@Test
public void testConvert()
{
final AddressModel addressModel = mock(AddressModel.class);
final PK pk = PK.parse("123");
final TitleModel titleModel = mock(TitleModel.class);
final CountryModel countryModel = mock(CountryModel.class);
given(addressModel.getPk()).willReturn(pk);
given(addressModel.getBillingAddress()).willReturn(Boolean.TRUE);
given(addressModel.getShippingAddress()).willReturn(Boolean.TRUE);
given(addressModel.getTitle()).willReturn(titleModel);
given(addressModel.getFirstname()).willReturn("firstName");
given(addressModel.getLastname()).willReturn("lastname");
given(titleModel.getName()).willReturn("titleName");
given(titleModel.getCode()).willReturn("titleCode");
given(addressModel.getCompany()).willReturn("companyName");
given(addressModel.getLine1()).willReturn("line1");
given(addressModel.getLine2()).willReturn("line2");
given(addressModel.getTown()).willReturn("town");
given(addressModel.getPostalcode()).willReturn("postalCode");
given(addressModel.getPhone1()).willReturn("phone");
given(addressModel.getEmail()).willReturn("email");
given(addressModel.getCountry()).willReturn(countryModel);
given(countryModel.getIsocode()).willReturn("countryCode");
given(countryModel.getName()).willReturn("countryName");
given(defaultAddressFormatConverter.convert(Mockito.any(AddressModel.class))).willReturn(
new StringBuilder("singleLineAddress"));
final AddressData addressData = addressConverter.convert(addressModel);
Assert.assertEquals("123", addressData.getId());
Assert.assertTrue(addressData.isBillingAddress());
Assert.assertTrue(addressData.isShippingAddress());
Assert.assertEquals("titleName", addressData.getTitle());
Assert.assertEquals("titleCode", addressData.getTitleCode());
Assert.assertEquals("firstName", addressData.getFirstName());
Assert.assertEquals("lastname", addressData.getLastName());
Assert.assertEquals("companyName", addressData.getCompanyName());
Assert.assertEquals("line1", addressData.getLine1());
Assert.assertEquals("line2", addressData.getLine2());
Assert.assertEquals("town", addressData.getTown());
Assert.assertEquals("postalCode", addressData.getPostalCode());
Assert.assertEquals("phone", addressData.getPhone());
Assert.assertEquals("email", addressData.getEmail());
Assert.assertEquals("countryCode", addressData.getCountry().getIsocode());
Assert.assertEquals("countryName", addressData.getCountry().getName());
}
}
If you are looking for a Facade that is using a Converter, check de.hybris.platform.commercefacades.customer.impl.DefaultCustomerFacadeTest
@UnitTest
public class DefaultCustomerFacadeTest
{
private static final String TEST_DUMMY = "dummy";
private static final String TEST_OLD_PASS = "oldPass";
private static final String TEST_NEW_PASS = "newPass";
private static final String TEST_USER_UID = "testUid";
private static final String TEST_TOKEN = "token";
private DefaultCustomerFacade defaultCustomerFacade;
@Mock
private UserService userService;
@Mock
private UserModel user;
@Mock
private CustomerAccountService customerAccountService;
@Mock
private ModelService mockModelService;
@Mock
private AbstractPopulatingConverter<AddressModel, AddressData> addressConverter;
@Mock
private AbstractPopulatingConverter<UserModel, CustomerData> customerConverter;
@Mock
private AddressReversePopulator addressReversePopulator;
@Mock
private AbstractPopulatingConverter<CreditCardPaymentInfoModel, CCPaymentInfoData> creditCardPaymentInfoConverter;
@Mock
private CommonI18NService commonI18NService;
@Mock
private StoreSessionFacade storeSessionFacade;
@Mock
private CartService cartService;
@Mock
private CommerceCartService commerceCartService;
@Mock
private UserFacade userFacade;
@Mock
private SessionService sessionService;
@Mock
private OrderFacade orderFacade;
@Mock
private CartCleanStrategy cartCleanStrategy;
private CustomerModel customerModel;
private CustomerModel guestCustomerModel;
private AddressModel addressModel;
private AddressModel addressModel2;
private AddressData addressData;
private CreditCardPaymentInfoModel creditCardPaymentInfoModel;
private CCPaymentInfoData ccPaymentInfoData;
private CustomerNameStrategy customerNameStrategy;
private CurrencyData defaultCurrencyData;
private LanguageData defaultLanguageData;
protected static class MockAddressModel extends AddressModel
{
private final long id;
public MockAddressModel(final long id)
{
this.id = id;
}
@Override
public PK getPk()
{
return de.hybris.platform.core.PK.fromLong(id);
}
}
@Before
public void setUp()
{
MockitoAnnotations.initMocks(this);
defaultCustomerFacade = new DefaultCustomerFacade();
defaultCustomerFacade.setUserService(userService);
defaultCustomerFacade.setModelService(mockModelService);
defaultCustomerFacade.setCustomerAccountService(customerAccountService);
defaultCustomerFacade.setAddressConverter(addressConverter);
defaultCustomerFacade.setCustomerConverter(customerConverter);
defaultCustomerFacade.setAddressReversePopulator(addressReversePopulator);
defaultCustomerFacade.setCreditCardPaymentInfoConverter(creditCardPaymentInfoConverter);
defaultCustomerFacade.setCommonI18NService(commonI18NService);
defaultCustomerFacade.setStoreSessionFacade(storeSessionFacade);
defaultCustomerFacade.setCartService(cartService);
defaultCustomerFacade.setCommerceCartService(commerceCartService);
defaultCustomerFacade.setUserFacade(userFacade);
defaultCustomerFacade.setSessionService(sessionService);
defaultCustomerFacade.setOrderFacade(orderFacade);
defaultCustomerFacade.setCartCleanStrategy(cartCleanStrategy);
customerNameStrategy = new DefaultCustomerNameStrategy();
defaultCustomerFacade.setCustomerNameStrategy(customerNameStrategy);
addressModel = new MockAddressModel(9999L);
addressModel2 = new MockAddressModel(8888L);
addressData = new AddressData();
addressData.setId("9999");
customerModel = new CustomerModel();
customerModel.setDefaultShipmentAddress(addressModel2);
creditCardPaymentInfoModel = new CreditCardPaymentInfoModel();
final List<CreditCardPaymentInfoModel> creditCards = new ArrayList<CreditCardPaymentInfoModel>();
creditCards.add(creditCardPaymentInfoModel);
ccPaymentInfoData = new CCPaymentInfoData();
guestCustomerModel = new CustomerModel();
guestCustomerModel.setUid(TEST_USER_UID + "|" + TEST_DUMMY);
guestCustomerModel.setDefaultShipmentAddress(addressModel);
guestCustomerModel.setDefaultPaymentAddress(addressModel2);
given(addressConverter.convert(addressModel)).willReturn(addressData);
given(creditCardPaymentInfoConverter.convert(creditCardPaymentInfoModel)).willReturn(ccPaymentInfoData);
given(userService.getCurrentUser()).willReturn(customerModel);
given(customerAccountService.getAddressForCode(customerModel, "9999")).willReturn(addressModel);
given(customerAccountService.getCreditCardPaymentInfos(customerModel, true)).willReturn(creditCards);
given(customerAccountService.getCreditCardPaymentInfoForCode(customerModel, "code")).willReturn(creditCardPaymentInfoModel);
given(mockModelService.create(CustomerModel.class)).willReturn(new CustomerModel());
defaultCurrencyData = new CurrencyData();
defaultCurrencyData.setIsocode("GBP");
defaultLanguageData = new LanguageData();
defaultLanguageData.setIsocode("en");
given(storeSessionFacade.getDefaultCurrency()).willReturn(defaultCurrencyData);
given(storeSessionFacade.getDefaultLanguage()).willReturn(defaultLanguageData);
}