Skip to content

Commit

Permalink
Merge pull request #31 from Veselovnd88/feature/47-test-container
Browse files Browse the repository at this point in the history
feature/47: testcontainers refactoring
  • Loading branch information
Veselovnd88 authored Jan 27, 2024
2 parents 28fe21c + 07b3e4f commit 1d1d3f7
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package ru.veselov.companybot.config;

import org.springframework.test.context.ContextConfiguration;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@ContextConfiguration(initializers = PostgresTestContainersInitializer.class)
public @interface EnableTestContainers {
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package ru.veselov.companybot.config;

import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.testcontainers.containers.PostgreSQLContainer;


/**
* Configuration class for managing test containers, it this situation we don't need use
* {@link org.testcontainers.junit.jupiter.Testcontainers } annotation
* on class, and {@link org.testcontainers.junit.jupiter.Container} annotation on the container itself.
* In this class we will start containers that will be available for all our tests
*/
public class PostgresTestContainersInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

static PostgreSQLContainer<?> postgresContainer = new PostgreSQLContainer<>("postgres:16")
.withDatabaseName("cbotDB");

static {
postgresContainer.start();
}


//initialize environment before dynamic property source applied
@Override
public void initialize(ConfigurableApplicationContext ctx) {
TestPropertyValues.of(
"spring.liquibase.enabled=true",
"spring.datasource.url=" + postgresContainer.getJdbcUrl(),
"spring.datasource.username=" + postgresContainer.getUsername(),
"spring.datasource.password=" + postgresContainer.getPassword()).applyTo(ctx.getEnvironment());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import ru.veselov.companybot.bot.handler.TelegramFacadeUpdateHandler;
import ru.veselov.companybot.cache.UserStateCache;
import ru.veselov.companybot.config.BotConfig;
import ru.veselov.companybot.config.PostgresTestContainersConfiguration;
import ru.veselov.companybot.config.EnableTestContainers;
import ru.veselov.companybot.entity.CompanyInfoEntity;
import ru.veselov.companybot.repository.CompanyInfoRepository;
import ru.veselov.companybot.util.MessageUtils;
Expand All @@ -30,7 +30,8 @@
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
@ActiveProfiles("test")
class AboutInfoUpdateBotIntegrationTest extends PostgresTestContainersConfiguration {
@EnableTestContainers
class AboutInfoUpdateBotIntegrationTest {

@MockBean
CompanyBot bot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import ru.veselov.companybot.bot.util.CallBackButtonUtils;
import ru.veselov.companybot.cache.UserStateCache;
import ru.veselov.companybot.config.BotConfig;
import ru.veselov.companybot.config.PostgresTestContainersConfiguration;
import ru.veselov.companybot.config.EnableTestContainers;
import ru.veselov.companybot.entity.ContactEntity;
import ru.veselov.companybot.entity.CustomerEntity;
import ru.veselov.companybot.repository.ContactRepository;
Expand All @@ -46,7 +46,8 @@
@ActiveProfiles("test")
@DirtiesContext
@Slf4j
class ContactBotIntegrationTest extends PostgresTestContainersConfiguration {
@EnableTestContainers
class ContactBotIntegrationTest {

@MockBean
CompanyBot bot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@
import ru.veselov.companybot.bot.handler.TelegramFacadeUpdateHandler;
import ru.veselov.companybot.cache.UserStateCache;
import ru.veselov.companybot.config.BotConfig;
import ru.veselov.companybot.config.PostgresTestContainersConfiguration;
import ru.veselov.companybot.config.EnableTestContainers;
import ru.veselov.companybot.repository.CustomerRepository;
import ru.veselov.companybot.util.MessageUtils;
import ru.veselov.companybot.util.UserActionsUtils;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@DirtiesContext
@ActiveProfiles("test")
class InfoCommandBotIntegrationTest extends PostgresTestContainersConfiguration {
@EnableTestContainers
class InfoCommandBotIntegrationTest {

@MockBean
CompanyBot bot;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import ru.veselov.companybot.bot.util.CallBackButtonUtils;
import ru.veselov.companybot.cache.UserStateCache;
import ru.veselov.companybot.config.BotConfig;
import ru.veselov.companybot.config.PostgresTestContainersConfiguration;
import ru.veselov.companybot.config.EnableTestContainers;
import ru.veselov.companybot.entity.ContactEntity;
import ru.veselov.companybot.entity.CustomerEntity;
import ru.veselov.companybot.entity.DivisionEntity;
Expand Down Expand Up @@ -58,7 +58,8 @@
@ActiveProfiles("test")
@DirtiesContext
@Slf4j
class InquiryBotIntegrationTest extends PostgresTestContainersConfiguration {
@EnableTestContainers
class InquiryBotIntegrationTest {

@Value("${bot.max-messages}")
private Integer maxMessages;
Expand Down Expand Up @@ -206,7 +207,7 @@ void shouldSaveWithCommonDivisionIfChosenDivisionAccidentallyWasNotFound() {
telegramFacadeUpdateHandler.processUpdate(userSendTextMessage);
divisionRepository.deleteAll();
pressContactInputContactAndPressSave();
Awaitility.await().pollDelay(Duration.ofMillis(1000)).until(() -> true);
Awaitility.await().pollDelay(Duration.ofMillis(3000)).until(() -> true);
Mockito.verify(bot, Mockito.times(3)).execute(Mockito.any(SendMessage.class));

List<InquiryEntity> allInquiries = inquiryRepository.findAll();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
import org.springframework.test.web.servlet.ResultActions;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import ru.veselov.companybot.config.BotMocks;
import ru.veselov.companybot.config.PostgresTestContainersConfiguration;
import ru.veselov.companybot.config.EnableTestContainers;
import ru.veselov.companybot.dto.DivisionDTO;
import ru.veselov.companybot.entity.DivisionEntity;
import ru.veselov.companybot.exception.util.ExceptionMessageUtils;
Expand All @@ -32,7 +32,8 @@
@DirtiesContext
@Import({BotMocks.class})
@ActiveProfiles("test")
class DivisionControllerIntegrationTest extends PostgresTestContainersConfiguration {
@EnableTestContainers
class DivisionControllerIntegrationTest {

@Autowired
MockMvc mockMvc;
Expand Down

0 comments on commit 1d1d3f7

Please sign in to comment.