SilentSender { public <T extends Serializable, Method extends BotApiMethod<T>> Optional<T> execute(Method method) { try { return Optional.ofNullable(sender.execute(method)); } catch (TelegramApiException e) { log.error("Could not execute bot API method", e); return Optional.empty(); } } SilentSender(MessageSender sender); Optional<Message> send(String message, long id); Optional<Message> sendMd(String message, long id); Optional<Message> forceReply(String message, long id); Optional<T> execute(Method method); void executeAsync(Method method, Callback callable);  }
@Test void returnsEmptyOnError() throws TelegramApiException { when(sender.execute(any())).thenThrow(TelegramApiException.class); Optional execute = silent.execute(null); assertFalse(execute.isPresent(), "Execution of a bot API method with execption results in a nonempty optional"); }
@Test void returnOptionalOnSuccess() throws TelegramApiException { String data = "data"; when(sender.execute(any())).thenReturn(data); Optional execute = silent.execute(null); assertEquals(data, execute.get(), "Silent execution resulted in a different object"); }