У меня есть пример реализации и тест, но я получаю сообщение об ошибке
java.lang.IllegalStateException: Failed to unwrap proxied object
...
E.g. thenReturn() may be missing.
Examples of correct stubbing:
when(mock.isOk()).thenReturn(true);
when(mock.isOk()).thenThrow(exception);
doThrow(exception).when(mock).someVoidMethod();
Hints:
1. missing thenReturn()
2. you are trying to stub a final method, which is not supported
3. you are stubbing the behaviour of another mock inside before 'thenReturn' instruction is completed
...
...
@Service
public class FirebaseService {
...
public FirebaseService(...) {
...
}
public FirebaseApp initializeApp() {
try {
...
InputStream inputStream = getServiceAccountFileInputStream();
...
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public InputStream getServiceAccountFileInputStream() throws IOException {
...
}
}
...
@ExtendWith(SpringExtension.class)
@RunWith(SpringRunner.class)
@SpringBootTest
public class FirebaseServiceTest {
...
@Autowired
private FirebaseService firebaseService;
...
@Test
public void whenGetAppIsEmpty_thenReturnFirebaseAppInstanceFromInitializeApp() throws IOException {
...
InputStream inputStream = mock(InputStream.class);
...
FirebaseService firebaseServiceSpy = spy(firebaseService);
// Specifically at this point
doReturn(FileInputStream.nullInputStream()).when(firebaseServiceSpy).getServiceAccountFileInputStream();
...
}
}
Я новичок в тестировании, поэтому ценю помощь всех, кто может помочь мне понять, что я сделал неправильно.
Спасибо
Я пытался шпионить за автоподключаемой зависимостью, как это, и ожидаю, что метод вернет нулевой входной поток...
...
FirebaseService firebaseServiceSpy = spy(firebaseService);
doReturn(FileInputStream.nullInputStream()).when(firebaseServiceSpy).getServiceAccountFileInputStream();
...
Подробнее здесь: https://stackoverflow.com/questions/769 ... -object-er