0Pricing
Spring Boot 4 Microservices & REST APIs · درس

التبعيات الحقيقية باستخدام Testcontainers

اختبر باستخدام قواعد بيانات حقيقية داخل الحاويات

التبعيات الحقيقية باستخدام Testcontainers درس مجاني في Spring Boot 4 Microservices & REST APIs على CoddyKit. هذا هو الدرس 4 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في Spring Boot 4 Microservices & REST APIs، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.

Why Testcontainers

Testcontainers runs real services — PostgreSQL, Redis, Kafka — inside Docker containers during tests.

You test against the real database, not an in-memory substitute, catching dialect and behavior differences.

Adding the Dependency

Add the Testcontainers JUnit and database modules to your build.

// build.gradle
testImplementation "org.testcontainers:junit-jupiter"
testImplementation "org.testcontainers:postgresql"

The @Testcontainers Annotation

@Testcontainers on the test class activates container lifecycle management.

@Testcontainers
@SpringBootTest
class UserRepositoryIT {
    // containers declared here
}

Declaring a PostgreSQLContainer

A static @Container field defines the database image to start.

@Container
static PostgreSQLContainer<?> postgres =
    new PostgreSQLContainer<>("postgres:16")
        .withDatabaseName("testdb")
        .withUsername("test")
        .withPassword("test");

Wiring the Datasource

Use @DynamicPropertySource to inject the container's generated JDBC URL into Spring.

@DynamicPropertySource
static void props(DynamicPropertyRegistry registry) {
    registry.add("spring.datasource.url",
        postgres::getJdbcUrl);
    registry.add("spring.datasource.username",
        postgres::getUsername);
    registry.add("spring.datasource.password",
        postgres::getPassword);
}

A Full Repository Test

Now repository tests run against real PostgreSQL.

@Test
void savesAndReads() {
    User saved = repository.save(new User("Alice"));
    assertThat(repository.findById(saved.getId()))
        .isPresent();
}

Static vs Instance Containers

A static container starts once for all tests in the class (faster). A non-static container restarts for each test (more isolation).

Other Container Types

Testcontainers has modules for many services.

@Container
static GenericContainer<?> redis =
    new GenericContainer<>("redis:7")
        .withExposedPorts(6379);

Reusing Containers

Enable container reuse to speed up local runs across test executions.

@Container
static PostgreSQLContainer<?> postgres =
    new PostgreSQLContainer<>("postgres:16")
        .withReuse(true);

Spring Boot Service Connections

Spring Boot 3.1+ can auto-configure the datasource from a container with @ServiceConnection, removing the need for @DynamicPropertySource.

@Container
@ServiceConnection
static PostgreSQLContainer<?> postgres =
    new PostgreSQLContainer<>("postgres:16");

Docker Requirement

Testcontainers needs a running Docker engine on the machine or CI agent. Without Docker the tests fail to start containers.

Quick Check

Test your understanding of Testcontainers.

Recap

You learned to test against real dependencies:

  • @Testcontainers + @Container start Docker services
  • PostgreSQLContainer for a real database
  • @DynamicPropertySource or @ServiceConnection wires the datasource
  • Requires a running Docker engine

الأسئلة الشائعة

هل درس «التبعيات الحقيقية باستخدام Testcontainers» مجاني؟

نعم — نص درس «التبعيات الحقيقية باستخدام Testcontainers» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة Spring Boot 4 Microservices & REST APIs، انتقل إلى CoddyKit PRO. تتضمن دورة Spring Boot 4 Microservices & REST APIs 4 دروس في المجموع.

ماذا ستتعلم في «التبعيات الحقيقية باستخدام Testcontainers»؟

اختبر باستخدام قواعد بيانات حقيقية داخل الحاويات تتمرن على Spring Boot 4 Microservices & REST APIs مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.

هل أحتاج إلى خبرة سابقة لأبدأ Spring Boot 4 Microservices & REST APIs؟

لا تُشترط خبرة سابقة. Spring Boot 4 Microservices & REST APIs على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 4 من أصل 4.

كم من الوقت يستغرق درس «التبعيات الحقيقية باستخدام Testcontainers»؟

معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.

هل يمكنني كتابة وتشغيل أكواد في درس Spring Boot 4 Microservices & REST APIs هذا؟

نعم. كل درس في Spring Boot 4 Microservices & REST APIs يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.

جميع الدروس في هذه الدورة

  1. اختبار الوحدات باستخدام JUnit وMockito
  2. اختبارات طبقة الويب باستخدام MockMvc
  3. اختبارات التكامل باستخدام @SpringBootTest
  4. التبعيات الحقيقية باستخدام Testcontainers
← العودة إلى Spring Boot 4 Microservices & REST APIs