الـ Stubbing مع Mockito
في هذا الدرس 4 موضوعات
هذا الدرس مخطط له. يعرض المخطط أدناه الموضوعات التي سيغطيها، ولم يُنشر الدرس الكامل بعد.
نص الدروس والتمارين باللغة الإنجليزية، بينما تبقى واجهة الموقع وعناوين الدروس بالعربية.
في هذا الدرس
make a mock return values or throw exceptions.
الموضوعات المخطط لها
@ExtendWith(MockitoExtension.class)with@Mockand@InjectMockssets things up. Verify the current Mockito version before recording.when(mock.method(arg)).thenReturn(value)stubs a return.thenThrow(...)simulates failure, such as a timeout.- Unstubbed methods return defaults (null, 0, empty collections).
- Use argument matchers (
any(),eq()) sparingly, and if one argument uses a matcher, all must.
تفاصيل المخطط المبدئي
عرض عملي
when(gateway.charge(any(), eq(new BigDecimal("50.00"))))
.thenReturn(PaymentResult.approved("tx-1"));
when(gateway.charge(any(), eq(new BigDecimal("99.00"))))
.thenThrow(new GatewayTimeoutException());اختبر فهمك
What does an unstubbed method returning a List give?
أظهر الإجابة
An empty list.
التطبيق
طبّق هذا الدرس بمختبر الوحدة وتمارينها واختبارها.
تعليم الدرس كمكتمل اختياري ويمكنك التراجع عنه في أي وقت. يبقى تقدمك محفوظًا في هذا المتصفح.