Add unit test for spring boot start configuration

This commit is contained in:
zhaoyi 2018-05-14 15:17:07 +08:00
parent dcc4f29ddd
commit d0651e60b4
3 changed files with 84 additions and 1 deletions

View File

@ -80,6 +80,19 @@
<artifactId>spring-boot-autoconfigure</artifactId> <artifactId>spring-boot-autoconfigure</artifactId>
<version>${spring-boot.version}</version> <version>${spring-boot.version}</version>
</dependency> </dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>2.0.2-beta</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -24,9 +24,13 @@ public class TelegramBotStarterConfiguration {
private final List<LongPollingBot> longPollingBots; private final List<LongPollingBot> longPollingBots;
private final List<WebhookBot> webHookBots; private final List<WebhookBot> webHookBots;
@Autowired
private TelegramBotsApi telegramBotsApi; private TelegramBotsApi telegramBotsApi;
@Autowired
public void setTelegramBotsApi(TelegramBotsApi telegramBotsApi) {
this.telegramBotsApi = telegramBotsApi;
}
public TelegramBotStarterConfiguration(@Autowired(required = false) List<LongPollingBot> longPollingBots, public TelegramBotStarterConfiguration(@Autowired(required = false) List<LongPollingBot> longPollingBots,
@Autowired(required = false) List<WebhookBot> webHookBots) { @Autowired(required = false) List<WebhookBot> webHookBots) {

View File

@ -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);
}
}