انتقل إلى المحتوى
أمين أبوزيد
JNR-045

اختبار التكامل (Integration testing)

  • 7 درس
  • 3 ساعة تدريب (تقدير)
في هذه الصفحة 8 موضوعات

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

نظرة عامة

الموضوعات

  • Spring Boot Test
  • الاختبار على قاعدة بيانات حقيقية
  • Testcontainers

تعلّم أولًا

3 ساعة تدريب (تقدير)

مخرجات التعلم

  1. Write @SpringBootTest and @DataJpaTest tests against a real PostgreSQL using Testcontainers.
  2. Use service connections so tests configure themselves from containers.
  3. Isolate test data between tests (transactions, cleanup, unique data).
  4. Explain why H2 is not a substitute for the production database.

الدروس

  1. 01ماذا تثبت اختبارات التكامل
  2. 02Testcontainers
  3. 03اختبارات المستودعات باستخدام @DataJpaTest
  4. 04اختبارات التطبيق الكامل
  5. 05عزل بيانات الاختبار
  6. 06عرض عملي: اختبارات تكامل Testcontainers لـ Shop
  7. 07عرض عملي: إصلاح تسرّب بيانات الاختبار

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

Lab

JNR-045-LAB1 · Real-database tests

Outcomes: 1–4 · Time: ~3 h Write repository tests for every custom query and an end-to-end order test with migrations applied, all against PostgreSQL via Testcontainers. Acceptance: suite passes from a clean machine with only Docker installed.

Exercises (assessment: add integration tests for an untested repository)

IDOutcomeTask
JNR-045-EX011Write a @DataJpaTest with a Testcontainers PostgreSQL for findByCategoryOrderByPrice.
JNR-045-EX021Write a @SpringBootTest (random port) that places an order and checks it is stored.
JNR-045-EX032Replace a @DynamicPropertySource setup with @ServiceConnection.
JNR-045-EX043Two tests insert the same unique email. Fix with unique data and cleanup.
JNR-045-EX053Explain why @Transactional on a test does not clean up data created through real HTTP.
JNR-045-EX064List three differences between H2 and PostgreSQL that can hide bugs.
JNR-045-EX071Make one container serve all test classes and time the suite before and after.
أظهر الحلول
  • EX01 @DataJpaTest @AutoConfigureTestDatabase(replace = NONE) @Testcontainers with a static @Container @ServiceConnection field; save products and assert order.
  • EX02 @SpringBootTest(webEnvironment = RANDOM_PORT), post through TestRestTemplate, then assert with the repository.
  • EX03 Remove the @DynamicPropertySource method and annotate the container field with @ServiceConnection.
  • EX04 Generate user-<uuid>@test.com per test; add repo.deleteAll() in @BeforeEach where needed.
  • EX05 The server handles the request on another thread with its own transaction, which commits; the test transaction does not cover it.
  • EX06 Examples: SQL dialect and functions, JSON/array/enum types, constraint and locking behavior, case sensitivity, migration scripts that only run on PostgreSQL.
  • EX07 A shared static container in an abstract base class or the singleton pattern; record before and after runtimes.
  • Outcome 4 is also covered by quiz Q09–Q10.

الاختبار

Outcomes: O1 Testcontainers tests · O2 service connections · O3 data isolation · O4 H2 versus production database.

اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.

  1. 1Integration tests mainly prove:O1
  2. 2Testcontainers needs which tool running?O1
  3. 3@DataJpaTest loads:O1
  4. 4To keep your PostgreSQL container with @DataJpaTest use:O1
  5. 5@ServiceConnection does what?O2
  6. 6Shared mutable data between tests causes:O3
  7. 7A good way to avoid data collisions:O3
  8. 8@Transactional on a test does not roll back data created:O3
  9. 9Why is H2 not a substitute for PostgreSQL?O4
  10. 10A query passing on H2 but failing in production shows:O4

الجزء العملي

Seven exercises JNR-045-EX01 to EX07 plus the lab; for the assessment, add integration tests for an untested repository. Pass: quiz 8/10, at least five exercises correct, and the suite green on a clean machine with only Docker.

أخطاء شائعة

Shared mutable data across tests; one container per test class without reuse.

التقييم

Add integration tests for an untested repository.

الصلة بالمسار الوظيفي

The foundation of a trustworthy CI pipeline.

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