Junit测试用例以模拟转换器

huangapple go评论82阅读模式
英文:

Junit test case to mock convertor

问题

我正在尝试使用'when---thenReturn'来模拟转换器方法。
在我的serviceTest文件中,但仍然抛出空指针异常。
有没有更好的方法来模拟转换器?
提前谢谢。

英文:

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.

答案1

得分: 1

以下是翻译好的代码部分:

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);
        // ... (其他部分省略,涉及地址属性的赋值和验证)
    }
}

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 部分省略)
    
    @Before
    public void setUp()
    {
        MockitoAnnotations.initMocks(this);
        defaultCustomerFacade = new DefaultCustomerFacade();
        defaultCustomerFacade.setUserService(userService);
        defaultCustomerFacade.setModelService(mockModelService);
        defaultCustomerFacade.setCustomerAccountService(customerAccountService);
        // ... (其他的 set 方法省略)
        
        customerModel = new CustomerModel();
        customerModel.setDefaultShipmentAddress(addressModel2);
        
        creditCardPaymentInfoModel = new CreditCardPaymentInfoModel();
        final List<CreditCardPaymentInfoModel> creditCards = new ArrayList<CreditCardPaymentInfoModel>();
        creditCards.add(creditCardPaymentInfoModel);
        ccPaymentInfoData = new CCPaymentInfoData();
        // ... (其他的模拟对象赋值部分省略)
        
        given(addressConverter.convert(addressModel)).willReturn(addressData);
        // ... (其他的 given 部分省略)
    }
}
英文:

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&lt;AddressModel, AddressData&gt; addressConverter;

	private final AddressPopulator addressPopulator = new AddressPopulator();

	@Mock
	private Map&lt;String, Converter&lt;AddressModel, StringBuilder&gt;&gt; addressFormatConverterMap;
	@Mock
	private Converter&lt;AddressModel, StringBuilder&gt; defaultAddressFormatConverter;

	@Before
	public void setUp()
	{
		MockitoAnnotations.initMocks(this);
		addressPopulator.setAddressFormatConverterMap(addressFormatConverterMap);
		addressPopulator.setDefaultAddressFormatConverter(defaultAddressFormatConverter);

		addressConverter = new ConverterFactory&lt;AddressModel, AddressData, AddressPopulator&gt;().create(AddressData.class,
				addressPopulator);
	}

	@Test
	public void testConvert()
	{
		final AddressModel addressModel = mock(AddressModel.class);
		final PK pk = PK.parse(&quot;123&quot;);
		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(&quot;firstName&quot;);
		given(addressModel.getLastname()).willReturn(&quot;lastname&quot;);
		given(titleModel.getName()).willReturn(&quot;titleName&quot;);
		given(titleModel.getCode()).willReturn(&quot;titleCode&quot;);
		given(addressModel.getCompany()).willReturn(&quot;companyName&quot;);
		given(addressModel.getLine1()).willReturn(&quot;line1&quot;);
		given(addressModel.getLine2()).willReturn(&quot;line2&quot;);
		given(addressModel.getTown()).willReturn(&quot;town&quot;);
		given(addressModel.getPostalcode()).willReturn(&quot;postalCode&quot;);
		given(addressModel.getPhone1()).willReturn(&quot;phone&quot;);
		given(addressModel.getEmail()).willReturn(&quot;email&quot;);
		given(addressModel.getCountry()).willReturn(countryModel);
		given(countryModel.getIsocode()).willReturn(&quot;countryCode&quot;);
		given(countryModel.getName()).willReturn(&quot;countryName&quot;);
		given(defaultAddressFormatConverter.convert(Mockito.any(AddressModel.class))).willReturn(
				new StringBuilder(&quot;singleLineAddress&quot;));
		final AddressData addressData = addressConverter.convert(addressModel);
		Assert.assertEquals(&quot;123&quot;, addressData.getId());
		Assert.assertTrue(addressData.isBillingAddress());
		Assert.assertTrue(addressData.isShippingAddress());
		Assert.assertEquals(&quot;titleName&quot;, addressData.getTitle());
		Assert.assertEquals(&quot;titleCode&quot;, addressData.getTitleCode());
		Assert.assertEquals(&quot;firstName&quot;, addressData.getFirstName());
		Assert.assertEquals(&quot;lastname&quot;, addressData.getLastName());
		Assert.assertEquals(&quot;companyName&quot;, addressData.getCompanyName());
		Assert.assertEquals(&quot;line1&quot;, addressData.getLine1());
		Assert.assertEquals(&quot;line2&quot;, addressData.getLine2());
		Assert.assertEquals(&quot;town&quot;, addressData.getTown());
		Assert.assertEquals(&quot;postalCode&quot;, addressData.getPostalCode());
		Assert.assertEquals(&quot;phone&quot;, addressData.getPhone());
		Assert.assertEquals(&quot;email&quot;, addressData.getEmail());
		Assert.assertEquals(&quot;countryCode&quot;, addressData.getCountry().getIsocode());
		Assert.assertEquals(&quot;countryName&quot;, 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 = &quot;dummy&quot;;
	private static final String TEST_OLD_PASS = &quot;oldPass&quot;;
	private static final String TEST_NEW_PASS = &quot;newPass&quot;;
	private static final String TEST_USER_UID = &quot;testUid&quot;;
	private static final String TEST_TOKEN = &quot;token&quot;;
	private DefaultCustomerFacade defaultCustomerFacade;


	@Mock
	private UserService userService;
	@Mock
	private UserModel user;
	@Mock
	private CustomerAccountService customerAccountService;
	@Mock
	private ModelService mockModelService;
	@Mock
	private AbstractPopulatingConverter&lt;AddressModel, AddressData&gt; addressConverter;
	@Mock
	private AbstractPopulatingConverter&lt;UserModel, CustomerData&gt; customerConverter;
	@Mock
	private AddressReversePopulator addressReversePopulator;
	@Mock
	private AbstractPopulatingConverter&lt;CreditCardPaymentInfoModel, CCPaymentInfoData&gt; 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(&quot;9999&quot;);

		customerModel = new CustomerModel();
		customerModel.setDefaultShipmentAddress(addressModel2);

		creditCardPaymentInfoModel = new CreditCardPaymentInfoModel();
		final List&lt;CreditCardPaymentInfoModel&gt; creditCards = new ArrayList&lt;CreditCardPaymentInfoModel&gt;();
		creditCards.add(creditCardPaymentInfoModel);
		ccPaymentInfoData = new CCPaymentInfoData();

		guestCustomerModel = new CustomerModel();
		guestCustomerModel.setUid(TEST_USER_UID + &quot;|&quot; + 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, &quot;9999&quot;)).willReturn(addressModel);
		given(customerAccountService.getCreditCardPaymentInfos(customerModel, true)).willReturn(creditCards);
		given(customerAccountService.getCreditCardPaymentInfoForCode(customerModel, &quot;code&quot;)).willReturn(creditCardPaymentInfoModel);
		given(mockModelService.create(CustomerModel.class)).willReturn(new CustomerModel());

		defaultCurrencyData = new CurrencyData();
		defaultCurrencyData.setIsocode(&quot;GBP&quot;);

		defaultLanguageData = new LanguageData();
		defaultLanguageData.setIsocode(&quot;en&quot;);

		given(storeSessionFacade.getDefaultCurrency()).willReturn(defaultCurrencyData);
		given(storeSessionFacade.getDefaultLanguage()).willReturn(defaultLanguageData);
	}

huangapple
  • 本文由 发表于 2020年4月9日 02:18:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/61107375.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定