我完全能够回答自己的问题。
在此示例中,将生成一个具有路易斯维尔地区性的地址,而其他字段则类似于“ 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);} }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)