Skip to content

Instantly share code, notes, and snippets.

@dmitriigo
Created February 26, 2021 13:40
Show Gist options
  • Save dmitriigo/e2d1a520e23d01959036010cabee1ebc to your computer and use it in GitHub Desktop.
Save dmitriigo/e2d1a520e23d01959036010cabee1ebc to your computer and use it in GitHub Desktop.
package ee.blakcat.weatherbotj.telegram;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.Spy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.boot.test.mock.mockito.SpyBeans;
import org.springframework.test.context.ActiveProfiles;
import org.telegram.telegrambots.meta.api.objects.CallbackQuery;
import org.telegram.telegrambots.meta.api.objects.Chat;
import org.telegram.telegrambots.meta.api.objects.Message;
import org.telegram.telegrambots.meta.api.objects.Update;
import ee.blakcat.weatherbotj.telegram.impl.callback.AddCityExecutor;
import ee.blakcat.weatherbotj.telegram.impl.message.ShowCityExecutor;
/**
* Created by IntelliJ IDEA.
* User:
* Date: 26/02/2021
* Time: 13:43
*/
@SpringBootTest
@ActiveProfiles("test")
class WeatherBotTest {
@Autowired
public WeatherBot weatherBot;
@MockBean
public AddCityExecutor addCityExecutor;
@MockBean
public ShowCityExecutor showCityExecutor;
@BeforeEach
void setUp() {
Mockito.when(showCityExecutor.getCommand()).thenCallRealMethod();
}
@Test
void onUpdateReceived() {
Update update = new Update();
CallbackQuery callbackQuery = new CallbackQuery();
callbackQuery.setData(addCityExecutor.getClass().getSimpleName()+" ");
Message message = new Message();
Chat chat = new Chat();
chat.setId(1L);
message.setChat(chat);
callbackQuery.setMessage(message);
update.setCallbackQuery(callbackQuery);
weatherBot.buildMessage(update);
Mockito.verify(addCityExecutor, Mockito.times(1)).execute(callbackQuery);
update.setCallbackQuery(null);
message.setText(showCityExecutor.getCommand());
update.setMessage(message);
weatherBot.buildMessage(update);
Mockito.verify(showCityExecutor, Mockito.times(1)).execute(message);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment