如何在Mockito中更改字符串的默认返回值?

如何在Mockito中更改字符串的默认返回值?,第1张

如何在Mockito中更改字符串的默认返回值?

我完全能够回答自己的问题。

在此示例中,将生成一个具有路易斯维尔地区性的地址,而其他字段则类似于“ address.getStreet1();”。

private Address createAddress(){    Address address = mock(Address.class, new StringAnswer() );    when(address.getLocality()).thenReturn("Louisville");    ISOCountry isoCountry = mock(ISOCountry.class);    when(isoCountry.getIsopre()).thenReturn("US");    when(address.getCountry()).thenReturn(isoCountry);    return address;}private class StringAnswer implements Answer<Object> {    @Override    public Object answer(InvocationOnMock invocation) throws Throwable {if( invocation.getMethod().getReturnType().equals(String.class)){    return invocation.toString();}else{    return Mockito.RETURNS_DEFAULTS.answer(invocation);}       }}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5475831.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存