اختبار التكامل (Integration testing)
في هذه الصفحة 8 موضوعات
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
نظرة عامة
الموضوعات
- Spring Boot Test
- الاختبار على قاعدة بيانات حقيقية
- Testcontainers
تعلّم أولًا
3 ساعة تدريب (تقدير)
مخرجات التعلم
- Write
@SpringBootTestand@DataJpaTesttests against a real PostgreSQL using Testcontainers. - Use service connections so tests configure themselves from containers.
- Isolate test data between tests (transactions, cleanup, unique data).
- Explain why H2 is not a substitute for the production database.
الدروس
المختبر والتمارين
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)
| ID | Outcome | Task |
|---|---|---|
| JNR-045-EX01 | 1 | Write a @DataJpaTest with a Testcontainers PostgreSQL for findByCategoryOrderByPrice. |
| JNR-045-EX02 | 1 | Write a @SpringBootTest (random port) that places an order and checks it is stored. |
| JNR-045-EX03 | 2 | Replace a @DynamicPropertySource setup with @ServiceConnection. |
| JNR-045-EX04 | 3 | Two tests insert the same unique email. Fix with unique data and cleanup. |
| JNR-045-EX05 | 3 | Explain why @Transactional on a test does not clean up data created through real HTTP. |
| JNR-045-EX06 | 4 | List three differences between H2 and PostgreSQL that can hide bugs. |
| JNR-045-EX07 | 1 | Make one container serve all test classes and time the suite before and after. |
أظهر الحلول
- EX01
@DataJpaTest @AutoConfigureTestDatabase(replace = NONE) @Testcontainerswith astatic @Container @ServiceConnectionfield; save products and assert order. - EX02
@SpringBootTest(webEnvironment = RANDOM_PORT), post throughTestRestTemplate, then assert with the repository. - EX03 Remove the
@DynamicPropertySourcemethod and annotate the container field with@ServiceConnection. - EX04 Generate
user-<uuid>@test.comper test; addrepo.deleteAll()in@BeforeEachwhere 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
staticcontainer 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.
اختبار ذاتي لك: لا توجد درجات، ولا يُحفظ أو يُرسل أي شيء. اختر إجابة ثم تحقق منها، ويمكنك المحاولة مرة أخرى كما تشاء.
الجزء العملي
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.