mirror of
https://github.com/OtusTeam/Spring.git
synced 2026-05-30 10:50:42 +00:00
2019-08 spring-05 shell test example implemented
This commit is contained in:
@@ -38,6 +38,36 @@
|
||||
<version>2.0.1.RELEASE</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.junit.vintage</groupId>
|
||||
<artifactId>junit-vintage-engine</artifactId>
|
||||
</exclusion>
|
||||
<exclusion>
|
||||
<groupId>junit</groupId>
|
||||
<artifactId>junit</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-api</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
<artifactId>junit-jupiter-engine</artifactId>
|
||||
<version>${junit-jupiter.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
+67
@@ -0,0 +1,67 @@
|
||||
package ru.otus.example.applicationeventsdemo.shell;
|
||||
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
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.shell.Availability;
|
||||
import org.springframework.shell.CommandNotCurrentlyAvailable;
|
||||
import org.springframework.shell.Shell;
|
||||
import org.springframework.test.annotation.DirtiesContext;
|
||||
import ru.otus.example.applicationeventsdemo.events.EventsPublisher;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.times;
|
||||
import static org.mockito.Mockito.verify;
|
||||
|
||||
@DisplayName("Тест команд shell ")
|
||||
@SpringBootTest
|
||||
class ApplicationEventsCommandsTest {
|
||||
|
||||
@MockBean
|
||||
private EventsPublisher eventsPublisher;
|
||||
|
||||
@Autowired
|
||||
private Shell shell;
|
||||
|
||||
private static final String GREETING_PATTERN = "Добро пожаловать: %s";
|
||||
private static final String DEFAULT_LOGIN = "stvort";
|
||||
private static final String CUSTOM_LOGIN = "Вася";
|
||||
private static final String COMMAND_LOGIN = "login";
|
||||
private static final String COMMAND_LOGIN_SHORT = "l";
|
||||
private static final String COMMAND_PUBLISH = "publish";
|
||||
private static final String COMMAND_PUBLISH_EXPECTED_RESULT = "Событие опубликовано";
|
||||
private static final String COMMAND_LOGIN_PATTERN = "%s %s";
|
||||
|
||||
@DisplayName(" должен возвращать приветствие для всех форм команды логина")
|
||||
@Test
|
||||
void shouldReturnExpectedGreetingAfterLoginCommandEvaluated() {
|
||||
String res = (String) shell.evaluate(() -> COMMAND_LOGIN);
|
||||
assertThat(res).isEqualTo(String.format(GREETING_PATTERN, DEFAULT_LOGIN));
|
||||
|
||||
res = (String) shell.evaluate(() -> COMMAND_LOGIN_SHORT);
|
||||
assertThat(res).isEqualTo(String.format(GREETING_PATTERN, DEFAULT_LOGIN));
|
||||
|
||||
res = (String) shell.evaluate(() -> String.format(COMMAND_LOGIN_PATTERN, COMMAND_LOGIN_SHORT, CUSTOM_LOGIN));
|
||||
assertThat(res).isEqualTo(String.format(GREETING_PATTERN, CUSTOM_LOGIN));
|
||||
}
|
||||
|
||||
@DisplayName(" должен возвращать CommandNotCurrentlyAvailable если при попытке выполнения команды publish пользователь выполнил вход")
|
||||
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
|
||||
@Test
|
||||
void shouldReturnCommandNotCurrentlyAvailableObjectWhenUserDoesNotLoginAfterPublishCommandEvaluated() {
|
||||
Object res = shell.evaluate(() -> COMMAND_PUBLISH);
|
||||
assertThat(res).isInstanceOf(CommandNotCurrentlyAvailable.class);
|
||||
}
|
||||
|
||||
@DisplayName(" должен возвращать статус выполнения команды publish и вызвать соответствующий метод сервиса есл икоманда выполнена после входа")
|
||||
@DirtiesContext(methodMode = DirtiesContext.MethodMode.BEFORE_METHOD)
|
||||
@Test
|
||||
void shouldReturnExpectedMessageAndFirePublishMethodAfterPublishCommandEvaluated() {
|
||||
shell.evaluate(() -> COMMAND_LOGIN);
|
||||
String res = (String) shell.evaluate(() -> COMMAND_PUBLISH);
|
||||
assertThat(res).isEqualTo(COMMAND_PUBLISH_EXPECTED_RESULT);
|
||||
verify(eventsPublisher, times(1)).publish();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
spring:
|
||||
shell:
|
||||
interactive:
|
||||
enabled: false
|
||||
Reference in New Issue
Block a user