From da63d938451ccdd86d2e425f1167881ab732d337 Mon Sep 17 00:00:00 2001 From: Evgeny Date: Tue, 24 Dec 2024 15:19:13 +0300 Subject: [PATCH] test: added tests --- ...icationExporterCompletableFutureTests.java | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 src/test/java/ru/dankoy/korvotoanki/KorvoToAnkiApplicationExporterCompletableFutureTests.java diff --git a/src/test/java/ru/dankoy/korvotoanki/KorvoToAnkiApplicationExporterCompletableFutureTests.java b/src/test/java/ru/dankoy/korvotoanki/KorvoToAnkiApplicationExporterCompletableFutureTests.java new file mode 100644 index 0000000..aa0591a --- /dev/null +++ b/src/test/java/ru/dankoy/korvotoanki/KorvoToAnkiApplicationExporterCompletableFutureTests.java @@ -0,0 +1,125 @@ +package ru.dankoy.korvotoanki; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.junit.jupiter.api.Assertions.assertAll; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.mockito.Mockito; +import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener; +import org.springframework.boot.test.context.runner.ApplicationContextRunner; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import ru.dankoy.korvotoanki.config.IoServiceConfig; +import ru.dankoy.korvotoanki.config.appprops.FilesProperties; +import ru.dankoy.korvotoanki.core.service.converter.AnkiConverterService; +import ru.dankoy.korvotoanki.core.service.exporter.ExporterServiceAnkiCompletableFuture; +import ru.dankoy.korvotoanki.core.service.filenameformatter.FileNameFormatterService; +import ru.dankoy.korvotoanki.core.service.fileprovider.FileProviderService; +import ru.dankoy.korvotoanki.core.service.state.StateService; +import ru.dankoy.korvotoanki.core.service.templatecreator.TemplateCreatorService; +import ru.dankoy.korvotoanki.core.service.vocabulary.VocabularyService; + +@DisplayName("Test completable future exporter bean context ") +class KorvoToAnkiApplicationExporterCompletableFutureTests { + + private final ApplicationContextRunner contextRunner = + new ApplicationContextRunner() + .withInitializer( + new ConditionEvaluationReportLoggingListener()) // to print out conditional config + // report to log + .withUserConfiguration(TestConfig.class, IoServiceConfig.class) + .withUserConfiguration(ExporterServiceAnkiCompletableFuture.class); + + @DisplayName("completable future exporter bean exists") + @Test + void asyncExporterServiceExists() { + + contextRunner + .withPropertyValues( + "korvo-to-anki.async=true", "korvo-to-anki.async-type=completable_future") + .run( + context -> + assertAll( + () -> + assertThat(context) + .hasSingleBean(ExporterServiceAnkiCompletableFuture.class))); + } + + @DisplayName("completable future exporter bean not exists") + @Test + void asyncExporterServiceNotExists() { + + contextRunner + .withPropertyValues("korvo-to-anki.async=false") + .run( + context -> + assertAll( + () -> + assertThat(context) + .doesNotHaveBean(ExporterServiceAnkiCompletableFuture.class))); + } + + @DisplayName("completable future exporter bean not exists") + @Test + void asyncExporterServiceTypeWhateverNotExists() { + + contextRunner + .withPropertyValues("korvo-to-anki.async=true", "korvo-to-anki.async-type=whatever") + .run( + context -> + assertAll( + () -> + assertThat(context) + .doesNotHaveBean(ExporterServiceAnkiCompletableFuture.class))); + } + + @Configuration // this annotation is not required here as the class is explicitly mentioned in + // `withUserConfiguration` method + protected static class TestConfig { + @Bean + public VocabularyService vocabularyService() { + return Mockito.mock( + VocabularyService.class); // this bean will be automatically autowired into tested beans + } + + @Bean + public AnkiConverterService ankiConverterService() { + return Mockito.mock( + AnkiConverterService + .class); // this bean will be automatically autowired into tested beans + } + + @Bean + public TemplateCreatorService templateCreatorService() { + return Mockito.mock( + TemplateCreatorService + .class); // this bean will be automatically autowired into tested beans + } + + @Bean + public FilesProperties filesProperties() { + return Mockito.mock( + FilesProperties.class); // this bean will be automatically autowired into tested beans + } + + @Bean + public StateService stateService() { + return Mockito.mock( + StateService.class); // this bean will be automatically autowired into tested beans + } + + @Bean + public FileProviderService fileProviderService() { + return Mockito.mock( + FileProviderService.class); // this bean will be automatically autowired into tested beans + } + + @Bean + public FileNameFormatterService fileNameFormatterService() { + return Mockito.mock( + FileNameFormatterService + .class); // this bean will be automatically autowired into tested beans + } + } +}