انتقل إلى المحتوى
أمين أبوزيد
درس مخطط لهJNR-045-L02

Testcontainers

  • الدرس 2 من 7
  • Junior
في هذا الدرس 4 موضوعات

هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.

نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.

في هذا الدرس

start a real PostgreSQL for tests from code.

الموضوعات المخطط لها

  • Testcontainers starts Docker containers from JUnit tests and removes them afterwards.
  • @Testcontainers with @Container manages the lifecycle.
  • Spring Boot's @ServiceConnection (Boot 3.1 or later; verify version before recording) configures the datasource from the container, with no URL copying.
  • Older style: @DynamicPropertySource sets properties from the container.
  • Make the container static so one container serves all tests in the class.
  • Share one container across classes (singleton or reuse) to save startup time.
تفاصيل المخطط المبدئي

عرض عملي

java
@Testcontainers
@SpringBootTest
class OrderIT {
    @Container @ServiceConnection
    static PostgreSQLContainer<?> db = new PostgreSQLContainer<>("postgres:16");
}

اختبر فهمك

What does @ServiceConnection replace?

أظهر الإجابة

Manually copying the container URL and credentials into properties.

التطبيق

طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.

المختبر والتمارين الاختبار

تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.

اكتب للبحث في المستويات والمراحل والوحدات والدروس.