From d0651e60b4e3e82d1478ef8b308a2f89f35785f5 Mon Sep 17 00:00:00 2001 From: zhaoyi Date: Mon, 14 May 2018 15:17:07 +0800 Subject: [PATCH] Add unit test for spring boot start configuration --- telegrambots-spring-boot-starter/pom.xml | 13 ++++ .../TelegramBotStarterConfiguration.java | 6 +- .../TestTelegramBotStarterConfiguration.java | 66 +++++++++++++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterConfiguration.java diff --git a/telegrambots-spring-boot-starter/pom.xml b/telegrambots-spring-boot-starter/pom.xml index b40ff9c9..72ed3705 100644 --- a/telegrambots-spring-boot-starter/pom.xml +++ b/telegrambots-spring-boot-starter/pom.xml @@ -80,6 +80,19 @@ spring-boot-autoconfigure ${spring-boot.version} + + + org.mockito + mockito-all + 2.0.2-beta + test + + + junit + junit + 4.11 + test + diff --git a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java index 0d7058e9..11e66af3 100644 --- a/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java +++ b/telegrambots-spring-boot-starter/src/main/java/org/telegram/telegrambots/starter/TelegramBotStarterConfiguration.java @@ -24,9 +24,13 @@ public class TelegramBotStarterConfiguration { private final List longPollingBots; private final List webHookBots; - @Autowired private TelegramBotsApi telegramBotsApi; + @Autowired + public void setTelegramBotsApi(TelegramBotsApi telegramBotsApi) { + this.telegramBotsApi = telegramBotsApi; + } + public TelegramBotStarterConfiguration(@Autowired(required = false) List longPollingBots, @Autowired(required = false) List webHookBots) { diff --git a/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterConfiguration.java b/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterConfiguration.java new file mode 100644 index 00000000..00a1e7cf --- /dev/null +++ b/telegrambots-spring-boot-starter/src/test/java/org/telegram/telegrambots/starter/TestTelegramBotStarterConfiguration.java @@ -0,0 +1,66 @@ +package org.telegram.telegrambots.starter; + +import com.google.common.collect.Lists; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; +import org.telegram.telegrambots.TelegramBotsApi; +import org.telegram.telegrambots.exceptions.TelegramApiRequestException; +import org.telegram.telegrambots.generics.LongPollingBot; +import org.telegram.telegrambots.generics.WebhookBot; + +import static org.mockito.Matchers.any; +import static org.mockito.Mockito.*; + +public class TestTelegramBotStarterConfiguration { + + @Mock + private TelegramBotsApi telegramBotsApi; + + @Rule + public MockitoRule mockitoRule = MockitoJUnit.rule(); + + @Test + public void TestRegisterBotsWithLongPollingBots() throws TelegramApiRequestException { + when(telegramBotsApi.registerBot(any(LongPollingBot.class))).then(Answers.RETURNS_MOCKS.get()); + LongPollingBot longPollingBot = mock(LongPollingBot.class); + TelegramBotStarterConfiguration configuration = new TelegramBotStarterConfiguration(Lists.newArrayList(longPollingBot), null); + configuration.setTelegramBotsApi(telegramBotsApi); + + configuration.registerBots(); + + verify(telegramBotsApi, times(1)).registerBot(longPollingBot); + verifyNoMoreInteractions(telegramBotsApi); + } + + @Test + public void TestRegisterBotsWithWebhookBots() throws TelegramApiRequestException { + doNothing().when(telegramBotsApi).registerBot(any(WebhookBot.class)); + WebhookBot webhookBot = mock(WebhookBot.class); + TelegramBotStarterConfiguration configuration = new TelegramBotStarterConfiguration(null, Lists.newArrayList(webhookBot)); + configuration.setTelegramBotsApi(telegramBotsApi); + + configuration.registerBots(); + + verify(telegramBotsApi, times(1)).registerBot(webhookBot); + verifyNoMoreInteractions(telegramBotsApi); + } + + @Test + public void TestRegisterBotsWithLongPollingBotsAndWebhookBots() throws TelegramApiRequestException { + doNothing().when(telegramBotsApi).registerBot(any(WebhookBot.class)); + LongPollingBot longPollingBot = mock(LongPollingBot.class); + WebhookBot webhookBot = mock(WebhookBot.class); + TelegramBotStarterConfiguration configuration = new TelegramBotStarterConfiguration(Lists.newArrayList(longPollingBot), Lists.newArrayList(webhookBot)); + configuration.setTelegramBotsApi(telegramBotsApi); + + configuration.registerBots(); + + verify(telegramBotsApi, times(1)).registerBot(longPollingBot); + verify(telegramBotsApi, times(1)).registerBot(webhookBot); + verifyNoMoreInteractions(telegramBotsApi); + } +}