From 323d7607d6200f5dfab9f8d3ecbac9c162aa85c9 Mon Sep 17 00:00:00 2001 From: Grig Alex Date: Tue, 20 Oct 2020 22:10:06 +0300 Subject: [PATCH] Create tests for CommandRegistry --- .../commands/CommandRegistryTest.java | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistryTest.java diff --git a/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistryTest.java b/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistryTest.java new file mode 100644 index 00000000..232ea85b --- /dev/null +++ b/telegrambots-extensions/src/test/java/org/telegram/telegrambots/extensions/bots/commandbot/commands/CommandRegistryTest.java @@ -0,0 +1,42 @@ +package org.telegram.telegrambots.extensions.bots.commandbot.commands; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.telegram.telegrambots.meta.api.objects.Message; +import org.telegram.telegrambots.meta.bots.AbsSender; + +class CommandRegistryTest { + + @Test + void should_executes_commandWithBotUsername() { + CommandRegistry registry = new CommandRegistry(true, () -> "BotUsername"); + IBotCommand command = Mockito.mock(IBotCommand.class); + Mockito.when(command.getCommandIdentifier()).thenReturn("command"); + registry.register(command); + AbsSender absSender = Mockito.mock(AbsSender.class); + Message message = Mockito.mock(Message.class); + Mockito.when(message.hasText()).thenReturn(true); + Mockito.when(message.getText()).thenReturn("/command@BotUsername I'll be test!"); + Assertions.assertTrue(registry.executeCommand(absSender, message), "Command must be executed"); + Mockito.verify(message).hasText(); + Mockito.verify(message).getText(); + Mockito.verify(command).processMessage( + Mockito.same(absSender), Mockito.same(message), Mockito.eq(new String[]{"I'll", "be", "test!"})); + } + + @Test + void should_throws_NPE_on_executes_commandWithNullableBotUsername() { + CommandRegistry registry = new CommandRegistry(true, () -> null); + IBotCommand command = Mockito.mock(IBotCommand.class); + Mockito.when(command.getCommandIdentifier()).thenReturn("command"); + registry.register(command); + AbsSender absSender = Mockito.mock(AbsSender.class); + Message message = Mockito.mock(Message.class); + Mockito.when(message.hasText()).thenReturn(true); + Mockito.when(message.getText()).thenReturn("/command@BotUsername ignore"); + NullPointerException exception = Assertions.assertThrows( + NullPointerException.class, () -> registry.executeCommand(absSender, message), "Bot username is null"); + Assertions.assertEquals("Bot username must not be null", exception.getMessage(), "Invalid exception message"); + } +} \ No newline at end of file