If I use @Spy, it will help me to mock methods. but it doesn't work for private variable initialization
FieldSetter.setField(discoveryService, discoveryService.getClass().getDeclaredField("discoveryURL"), discoveryUrl);
If I remove @spy, FieldSetter works to initialize mock private variables. My code with @spy:
@InjectMocks
/*line 5*/ @Spy
private Class object;
@Test
void getFetchDiscoveryTest() throws IOException, NoSuchFieldException {
String discoveryUrl = "https://ffc-onenote.officeapps.live.com/hosting/discovery";
/*line 15*/ FieldSetter.setField(object, object.getClass().getDeclaredField("discoveryURL"), discoveryUrl);
/*line 16*/ doThrow(IOException.class).when(object).getBytes(any());
/*line 17*/ when(object.getBytes(any())).thenThrow(new IOException("IO issue"));
assertThrows(Exception.class, () -> object.getWopiDiscovery());
here if I putted line #5, then line #15 not works and line #16 working good. why If i have @spy, FieldSetter not works. how to make FieldSetter working for @spy as well ?